-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy path07_array_functions.php
More file actions
80 lines (50 loc) · 1.3 KB
/
Copy path07_array_functions.php
File metadata and controls
80 lines (50 loc) · 1.3 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
<?php
$fruits= ['apples','orange','pear','banana'];
//Get length
// echo count($fruits);
//search array
// var_dump(in_array('apple',$fruits));
//add to an array
// $fruits = ['grape'];
//add to the end
// array_push($fruits,'banana');
//add to the beginning
// array_unshift($fruits,'mango');
foreach($fruits as $fruit){
// echo $fruit;
}
//remove from an array - from the end
// array_pop($fruits);
//remove from the beginning
// array_shift($fruits);
//specific element gone
// unset($fruits[0]);
//split into 2 chunks
// print_r($fruits);
// $chunked_array = array_chunk($fruits,2);
// $print_r($chunked_array);
$arr1 = [1,2,3];
$arr2 = [4,5,6];
$arr3 = array_merge($arr1,$arr2);
//spread operator
$arr4 = [...$arr1,...$arr2];
// print_r($arr3);
// 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($number){
return "Number ${number}";
},$numbers);
// print_r($newNumbers);
$lessThanTen = array_filter($numbers,fn($number)=>$number<10);
// print_r($lessThanTen);
$sum = array_reduce($numbers,fn($carry,$number)=>$carry+$number);
var_dump($sum);