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
<?php
// 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
<?php
// 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
<?php
//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
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
Menu
1. Create
2. Display
3. Edit
4. Delete
0. Exit
Enter Menu: 8 (User input)
Invalid Input

Menu
1. Create
2. Display
3. Edit
4. Delete
0. Exit
Enter Menu: 1 (User input)
Index 0: Azhar (User input)
Index 1: Jordie (User input)
Index 2: Ridho (User input)
Index 3: Ardi (User input)
Index 4: Clementius (User input)
Index 5: Jasmine (User input)
Index 6: (User input)
Data Succesfully Added

Menu
1. Create
2. Display
3. Edit
4. Delete
0. Exit
Enter Menu: 2 (User input)
Index 0: Azhar
Index 1: Jordie
Index 2: Ridho
Index 3: Ardi
Index 4: Clementius
Index 5: Jasmine
Data Successfully Displayed

Menu
1. Create
2. Display
3. Edit
4. Delete
0. Exit
Enter Menu: 3 (User input)
Enter the index you want to edit: 2 (User input)
Enter the new name: Dian (User input)
Data in the list has been successfully changed

Menu
1. Create
2. Display
3. Edit
4. Delete
0. Exit
Enter Menu: 1 (User input)
Index 0: Azhar
Index 1: Jordie
Index 2: Dian
Index 3: Ardi
Index 4: Clementius
Index 5: Jasmine
Data Successfully Displayed

Menu
1. Create
2. Display
3. Edit
4. Delete
0. Exit
Enter Menu: 4 (User input)
Enter the index you want to delete: 4 (User input)
Data Successfully Deleted

Menu
1. Create
2. Display
3. Edit
4. Delete
0. Exit
Enter Menu: 1 (User input)
Index 0: Azhar
Index 1: Jordie
Index 2: Dian
Index 3: Ardi
Index 4: Jasmine
Data Successfully Displayed

Menu
1. Create
2. Display
3. Edit
4. Delete
0. Exit
Enter Menu: 0 (User input)
Thank you for using my program