-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy path05_arrays.php
More file actions
159 lines (127 loc) · 3.24 KB
/
05_arrays.php
File metadata and controls
159 lines (127 loc) · 3.24 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
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
<?php
// // Create array
// $fruits = ["banana", "mango", "apple", "orange"];
// // Print the whole array
// echo '<pre>';
// var_dump($fruits);
// echo '<pre>';
// // Get element by index
// echo $fruits[1] . '<br>';
// // Set element by index
// $fruits[0] = 'peach';
// echo '<pre>';
// var_dump($fruits);
// echo '<pre>';
// // Check if array has element at index 2
// isset($fruits[3]); //false
// // Append element
// $fruits[] = 'banana';
// echo '<pre>';
// var_dump($fruits);
// echo '<pre>';
// // Print the length of the array
// echo count($fruits) . '<br>';
// // Add element at the end of the array
// array_push($fruits, 'foo');
// echo '<pre>';
// var_dump($fruits);
// echo '<pre>';
// // Remove element from the end of the array
// echo array_pop($fruits);
// echo '<pre>';
// var_dump($fruits);
// echo '</pre>';
// // Add element at the beginning of the array
// array_unshift($fruits, 'bik');
// echo '<pre>';
// var_dump($fruits);
// echo '</pre>';
// // Remove element from the beginning of the array
// echo array_shift($fruits);
// echo '<pre>';
// var_dump($fruits);
// echo '</pre>';
// // Split the string into an array
// $string = "banana, Apple,peace";
// echo '<pre>';
// var_dump(explode(",", $string));
// echo '</pre>';
// // Combine array elements into string
// echo implode("&", $fruits);
// // 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('mango', $fruits));
// echo '</pre>';
// // Merge two arrays
// $vegetables = ["potato", "cucumber"];
// echo '<pre>';
// var_dump(array_merge($fruits, $vegetables));
// var_dump([...$fruits, ...$vegetables]);
// echo '<pre>';
// // Sorting of array (Reverse order also)
// echo '<pre>';
// var_dump($fruits);
// echo '</pre';
// rsort($fruits);
// echo '<per>';
// var_dump($fruits);
// echo '</pre>';
// https://www.php.net/manual/en/ref.array.php
// ============================================
// Associative arrays
// ============================================
// Create an associative array
$person = [
'name' => 'bikash',
'surname' => 'yadav',
'age' => 30,
'hobbies' => ['cri', 'video game' . 'travel']
];
echo '<pre>';
var_dump($person);
//print_r($person)
echo '</pre>';
// Get element by key
echo $person['name'] . '<br>';
// Set element by key
$person['channel'] = 'darkbik';
echo '<pre>';
var_dump($person);
echo '</pre>';
//// Null coalescing assignment operator. Since PHP 7.4
// if (!isset($person['address'])) {
// $person['address'] = 'unknown';
// }
// echo '<pre>';
// var_dump($person);
// echo '</pre>';
$person['address'] ??= 'unknown';
echo '<pre>';
var_dump($person);
echo '</pre>';
// Check if array has specific key
// 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);
echo '<pre>';
var_dump($person);
echo '</pre>';
// Two dimensional arrays
$todos = [
['title' => 'todo title 1', 'completed' => true],
['title' => 'todo title 2', 'completed' => false]
];
echo '<pre>';
var_dump($todos);
echo '</pre>';