-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy path017-adding-examples-for-loops.php
More file actions
30 lines (27 loc) · 988 Bytes
/
017-adding-examples-for-loops.php
File metadata and controls
30 lines (27 loc) · 988 Bytes
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
<?php
for($i = 0; $i < 10; $i++) {
echo $i . "\n";
}
$fruits = ["apple", "banana", "orange"];
for($i = 0; $i < count($fruits); $i++) {
echo $fruits[$i] . "\n";
}
$myArray = [
"fruits" => ["apple", "banana", "orange"],
"vegetables" => ["carrot", "potato", "onion"],
"meat" => ["beef", "pork", "chicken"],
"dairy" => ["milk", "yogurt", "cheese"],
"drinks" => ["water", "tea", "coffee"],
"food" => ["pizza", "burger", "salad"],
"sports" => ["tennis", "soccer", "basketball"]
];
// Using a for loop with a pre-calculated array length to iterate through the array.
// // This approach stores the array length in a variable ($iMax) to avoid recalculating it on each iteration, improving performance.
$keys = array_keys($myArray);
for($i = 0, $iMax = count($myArray); $i < $iMax; $i++) {
echo $keys[$i] . " => ";
for($j = 0, $jMax = count($myArray[$keys[$i]]); $j < $jMax; $j++) {
echo $myArray[$keys[$i]][$j] . ", ";
}
echo "\n";
}