Algorithms and Programming PHP 6
Array/List
An array (or list) is a collection that contains multiple pieces of data. Generally, an array has indices (a sequence in the collection of data) that start from 0. To declare an array, you can add square brackets “[]” after the variable name and initialize it with curly braces “{},” separating each data element with a comma “,”.
Basic Example of Using an Array:1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
// Create Empty Array/List
$buah = array();
$hobi = [];
//Create Array/List with a value
$iniList = [25, 50, 75, 100];
echo($iniList[0]);
echo("\n");
echo($iniList[3]);
//Delete 25
unset($iniList[0]);
//Adding value of array/list in 3rd index
$iniList[3] = 125;
//Adding value of array/list in last index
$iniList[] = 150;
Associative Array/List
An associative array is an array whose index does not use numbers or digits. The index of an associative array is in the form of a keyword.
Basic Example of Using an Associative Array:1
2
3
4
5
6
7
8
9
10
11
// Create Associative Array/List
$artikel = [
"judul" => "Belajar Pemrograman PHP",
"penulis" => "Zulma",
"view" => 128
];
echo($artikel["judul"]."\n");
echo($artikel["penulis"]."\n");
Multidimension Array/List
A multidimensional array is an array that has more than one dimension. It is commonly used to create matrices, graphs, and other complex data structures.
Basic Example of Using an Multidimension Array:1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
//Create Multidimension Array
$artikel = [
[
"judul" => "HTML & CSS",
"penulis" => "Zulma"
],
[
"judul" => "JavaScript Algorithm",
"penulis" => "Zulma"
],
[
"judul" => "PHP Algorithm",
"penulis" => "Zulma"
]
];
// Showing Array
foreach($artikel as $post){
echo($post["judul"]."\n");
echo($post["penulis"]."\n");
}
Array/List Task
Create a list of names and implement a menu that can display, edit, and delete the list items, allowing the program to run repeatedly. Here is the expected output:
1 | Menu |