-
Notifications
You must be signed in to change notification settings - Fork 52
Expand file tree
/
Copy path05_arrays.php
More file actions
143 lines (112 loc) · 3.01 KB
/
05_arrays.php
File metadata and controls
143 lines (112 loc) · 3.01 KB
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
<?php
// Create array
$fruits = ["Banana", "Apple", "Orange"];
// Print the whole array
echo '<pre>';
var_dump($fruits); // print_r
echo '</pre>';
// Get element by index
echo $fruits[1].'<br>';
// Set element by index
$fruits[0] = "Peach";
// Check if array has element at index 2
echo '<pre>';
var_dump(isset($fruits[2])); // Change age into 5
echo '</pre>';
// Append element
$fruits[] = 'Peach';
echo $fruits[3].'<br>';
// Print the length of the array
echo count($fruits).'<br>';
// Add element at the end of the array
array_push($fruits, 'Foo');
// Remove element from the end of the array
array_pop($fruits);
echo '<pre>';
var_dump($fruits);
echo '</pre>';
// Add element at the beginning of the array
array_unshift($fruits, 'Apple');
// Remove element from the beginning of the array
array_shift($fruits);
// Split the string into an array
$string = "Banana,Apple,Peach";
echo '<pre>';
var_dump(explode(",", $string));
echo '</pre>';
print_r($fruits);
// Combine array elements into string
echo implode(",", $fruits).'<br>';
// Check if element exist in the array
echo '<pre>';
var_dump(in_array('Apple', $fruits));
echo '</pre>';
// Search element index in the array
echo '<pre>';
var_dump(array_search("Peach", $fruits));
echo '</pre>';
// Merge two arrays
$vegetables = ['Potato', 'Cucumber'];
echo '<pre>';
var_dump(array_merge($fruits, $vegetables));
var_dump([...$fruits, ...$vegetables]); // Since PHP 7.4
echo '</pre>';
// Sorting of array (Reverse order also)
sort($fruits); //sort, rsort, usort
echo '<pre>';
var_dump($fruits);
echo '</pre>';
// Filter, map, reduce of array
$numbers = [1, 2, 3, 4, 5, 6, 7, 8];
$evens = array_filter($numbers, function($n){ // fn($n) => $n % 2 === 0
return $n % 2 === 0;
});
echo '<pre>';
var_dump($evens);
echo '</pre>';
$squares = array_map(fn($n) => $n + 1, $numbers);
echo '<pre>';
var_dump($squares);
echo '</pre>';
$sum = array_reduce($numbers, fn($carry, $item) => $carry + $item);
echo $sum.'<br>';
// https://www.php.net/manual/en/ref.array.php
// ============================================
// Associative arrays
// ============================================
// Create an associative array
$person = [
'name' => 'Brad',
'surname' => 'Traversy',
'age' => 30,
'hobbies' => ['Tennis', 'Video Games'],
];
// Get element by key
echo $person['name'].'<br>';
// Set element by key
$person['channel'] = 'TraversyMedia';
// Check if array has specific key
echo '<pre>';
var_dump(isset($person['age'])); // Change age into "location"
echo '</pre>';
// 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
ksort($person); // ksort, krsort, asort, arsort
echo '<pre>';
var_dump($person);
echo '</pre>';
// Two dimensional arrays
$todoItems = [
['title' => 'Todo1', 'completed' => true],
['title' => 'Todo 2', 'completed' => false],
];
echo '<pre>';
var_dump($todoItems);
echo '</pre>';