-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy path07_array_functions.php
67 lines (46 loc) · 1.42 KB
/
07_array_functions.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
<?php
/* --------- Array Functions -------- */
/*
Functions to work with arrays
https://www.php.net/manual/en/ref.array.php
*/
$fruits = ['apple', 'banana', 'orange', 'plum', 'dates', 'mango'];
// Get length of an array
// echo count($fruits); // 6
// Search array for a value
// var_dump(in_array('apple', $fruits)); // true
// Add to array
$fruits[] = 'pineapple'; // Push to the end of array
array_push($fruits, 'grapes', 'lemon');
array_unshift($fruits, 'watermelon'); // Push to the beginning of array
// Remove from array
array_pop($fruits); // Remove last element
array_shift($fruits); // Remove first element
// unset($fruits[1]); // Remove specific element
// Split into 2 chunks
// $chunked_array = array_chunk($fruits, 3);
// print_r($chunked_array);
// print_r($fruits);
$arr1 = [1, 2, 3];
$arr2 = [4, 5, 6];
$arr3 = array_merge($arr1, $arr2);
$arr4 = [...$arr1, ...$arr2];
// print_r($arr4);
$a = ['green', 'red', 'yellow'];
$b = ['avocado', 'apple', 'banana'];
$c = array_combine($a, $b);
// print_r($c);
$keys = array_keys($c);
// print_r($keys);
$flipped = array_flip($c);
// print_r($flipped);
$numbers = range(1, 20);
// print_r($numbers);
$newNumbers = array_map(function ($num) {
return "Number $num";
}, $numbers);
// print_r($newNumbers);
$lessThan10 = array_filter($numbers, fn ($num) => $num <= 10);
// print_r($lessThan10);
$sum = array_reduce($numbers, fn ($carry, $num) => $carry + $num);
echo $sum;