-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy path026-arrays-array_pop-array_shift-unset-array_push-unshift-array_unique.php
More file actions
52 lines (44 loc) · 1.65 KB
/
026-arrays-array_pop-array_shift-unset-array_push-unshift-array_unique.php
File metadata and controls
52 lines (44 loc) · 1.65 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
<?php
function arrayOutput($nameArray, $array) {
echo "{$nameArray} = [";
foreach ($array as $value) {
echo $value . ",";
}
echo "]\n";
}
$colors = ["red", "green", "blue", "yellow", "black", "white", "brown"];
arrayOutput("colors", $colors);
$arrayPop = array_pop($colors);
echo "arrayPop = {$arrayPop} \n";
arrayOutput("colors", $colors);
$arrayShift = array_shift($colors);
echo "arrayShift = {$arrayShift} \n";
arrayOutput("colors", $colors);
$variableArrayPush = "pink";
$arrayPush =array_push($colors, $variableArrayPush);
echo "arrayPush([$variableArrayPush}) = {$arrayPush} \n";
arrayOutput("colors", $colors);
$variableArrayUnshift = "orange";
$arrayUnshift = array_unshift($colors, $variableArrayUnshift);
echo "arrayUnshift({$variableArrayUnshift}) = {$arrayUnshift} \n";
arrayOutput("colors", $colors);
echo "unset({$colors[0]})\n";
unset($colors[0]);
arrayOutput("colors", $colors);
$variableArrayPush1 = "black";
$variableArrayPush2 = "yellow";
array_push($colors, $variableArrayPush1);
array_push($colors, $variableArrayPush1);
array_push($colors, $variableArrayPush1);
array_push($colors, $variableArrayPush2);
array_push($colors, $variableArrayPush2);
echo "arrayPush([$variableArrayPush1}) = {$arrayPush} \n";
echo "arrayPush([$variableArrayPush1}) = {$arrayPush} \n";
echo "arrayPush([$variableArrayPush1}) = {$arrayPush} \n";
echo "arrayPush([$variableArrayPush2}) = {$arrayPush} \n";
echo "arrayPush([$variableArrayPush2}) = {$arrayPush} \n";
$colorsUnique = array_unique($colors);
arrayOutput("colors", $colors);
arrayOutput("colorsUnique", $colorsUnique);
$colors[] = "blue"; //faster than array_push()
arrayOutput("colors", $colors);