-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy patharray.php
More file actions
97 lines (85 loc) · 2.03 KB
/
array.php
File metadata and controls
97 lines (85 loc) · 2.03 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
<!-- array -->
<!-- types of arrays -->
<!-- indexed arrays -->
<?php
$fruits = array("apple", "banana", "orange");
echo "I like " . $fruits[0] . ", " . $fruits[1] . " and " . $fruits[2] . ".";
?>
<!-- associative arrays -->
<?php
$age = array("Peter"=>"35", "Ben"=>"37", "Joe"=>"43");
echo "Peter is " . $age['Peter'] . " years old.";
?>
<!-- multi-dimensional arrays -->
<?php
$cars = array
(
array("Volvo",22,18),
array("BMW",15,13),
array("Saab",5,2),
array("Land Rover",17,15)
);
for ($row = 0; $row < 4; $row++) {
echo "<p><b>Row number $row</b></p>";
echo "<ul>";
for ($col = 0; $col < 3; $col++) {
echo "<li>".$cars[$row][$col]."</li>";
}
echo "</ul>";
}
?>
<!-- modify multi dimensional array elements -->
<?php
// Input multidimensional array
$array = [
'fruits' => [
'apple' => 1,
'banana' => 2,
],
'vegetables' => [
'carrot' => 3,
'lettuce' => 4,
],
];
// Modify existing value
$array['fruits']['banana'] = 5;
// Add new value
$array['vegetables']['tomato'] = 6;
// Print the modified array
print_r($array['vegetables']['tomato'] );
?>
<!--remove multi dimensional array elements-->
<?php
// Input multidimensional array
$array = [
'fruits' => [
'apple' => 1,
'banana' => 5,
],
'vegetables' => [
'carrot' => 3,
'lettuce' => 4,
'tomato' => 6,
],
];
// Remove 'banana' from the fruits array
unset($array['fruits']['banana']);
// Remove 'lettuce' from the vegetables array
unset($array['vegetables']['lettuce']);
// Print the modified array
print_r($array);
?>
<!--loop through a multidimensional array(for each)-->
<!-- for each loop multidimensional array-->
<?php
$cars = array
(
array("Volvo",22,18),
array("BMW",15,13),
array("Saab",5,2),
array("Land Rover",17,15)
);
foreach( $cars as $car){
echo "I want to buy ".$car[0]." with ".$car[1]." miles per gallon and ".$car[2]." miles per hour"."\n";
}
?>