-
Notifications
You must be signed in to change notification settings - Fork 0
/
05_arrays.php
164 lines (136 loc) · 3.4 KB
/
05_arrays.php
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
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
<?php
$hr = "<hr>";
// Create array
// $fruits = array("Banana", "Apple", "Orange");
$fruits = ["Banana", "Apple", "Orange"];
echo "<h3>Print the whole array</h3>";
echo "<pre>";
var_dump($fruits);
echo "</pre>";
echo $hr;
echo "<h3>Get element by index</h3>";
echo $fruits[0] . "<br>";
echo $hr;
echo "<h3>Set element by index</h3>";
$fruits[0] = "Peach";
echo "<pre>";
var_dump($fruits);
echo "</pre>";
echo $hr;
echo "<h3>Check if array has element at index 2</h3>";
echo "<pre>";
var_dump(isset($fruits[2]));
echo "</pre>";
echo $hr;
echo "<h3>Append element</h3>";
$fruits[] = "Mango";
echo "<pre>";
var_dump($fruits);
echo "</pre>";
echo $hr;
echo "<h3>Print the length of the array</h3>";
echo count($fruits) . "<br>";
echo $hr;
echo "<h3>Add element at the end of the array</h3>";
array_push($fruits, "Papaya");
echo "<pre>";
var_dump($fruits);
echo "</pre>";
echo $hr;
echo "<h3>Remove element from the end of the array</h3>";
array_pop($fruits);
echo "<pre>";
var_dump($fruits);
echo "</pre>";
echo $hr;
echo "<h3>Add element at the beginning of the array</h3>";
array_unshift($fruits, "Apple");
echo "<pre>";
var_dump($fruits);
echo "</pre>";
echo $hr;
// Remove element from the beginning of the array
array_unshift($fruits);
// Split the string into an array
$string = "Banana, Apple, Peach";
echo "<pre>";
var_dump(explode(",", $string));
echo "</pre>";
echo $hr;
// Combine array elements into string
echo implode(", ", $fruits);
// Check if element exist in the array
in_array("Apple", $fruits) . "<br>";
// Search element index in the array
echo "<pre>";
var_dump($fruits);
echo "</pre>";
echo array_search("Apple", $fruits);
// Merge two arrays
$vegetables = ["Potato", "Cucumber"];
echo "<pre>";
var_dump(array_merge($fruits, $vegetables));
var_dump([...$fruits, ...$vegetables]);
echo "</pre>";
echo $hr;
// Sorting of array (Reverse order also)
sort($fruits);
echo "<pre>";
var_dump($fruits);
echo "</pre>";
echo $hr;
// Filter, map, reduce of array
$number = [1, 2, 3, 4, 5, 6, 7, 8];
// $evens = array_filter($number, function ($n) {
// return $n % 2 === 0;
// });
$evens = array_filter($number, fn ($n) => $n % 2 === 0);
echo "<pre>";
var_dump($evens);
echo "</pre>";
$squares = array_map(fn ($n) => $n * $n, $number);
echo "<pre>";
var_dump($squares);
echo "</pre>";
echo array_reduce($number, fn ($carry, $item) => $carry + $item);
// https://www.php.net/manual/en/ref.array.php
// ============================================
// Associative arrays
// ============================================
// Create an associative array
$person = [
"name" => "Brand",
"surname" => "Traversy",
"age" => 30,
"hobbies" => ["Tennis", "Video Games"]
];
// Get element by key
echo $person["name"] . "<br>";
echo $person["age"] . "<br>";
// Set element by key
$person["channel"] = "Traversy Media";
// Check if array has specific key
isset($person["age"]);
isset($person["address"]);
// Print the keys of the array
echo "<pre>";
var_dump(array_keys($person));
echo "</pre>";
// Print the values of the array
echo "<pre>";
var_dump(array_values($person));
echo "</pre>";
// Sorting associative arrays by values, by keys
asort($person);
echo "<pre>";
var_dump($person);
echo "</pre>";
// Two dimensional arrays
$todoItems = [
["title" => "item 1", "completed" => true],
["title" => "item 2", "completed" => false],
["title" => "item 3", "completed" => true],
];
echo "<pre>";
var_dump($todoItems);
echo "</pre>";