SakhaliaNetHome PageMilitary HistoryVorKutaAcceptance of cookiesAcceptance of cookies

PHP Tutorial :: Arrays (V)

PHP Example #46

Manipulating multi-dimensional arrays

This example shows certain manipulation of the data contained in multi-dimensional arrays.

<?php
$prices['dinner']['Sweet Corn and Asparagus'] = 12.50;
$prices['lunch']['Cashew Nuts and White Mushrooms'] = 4.95;
$prices['dinner']['Braised Bamboo Fungus'] = 8.95;
$prices['dinner']['total'] =
$prices['dinner']['Sweet Corn and Asparagus'] +
$prices['dinner']['Braised Bamboo Fungus'];
$specials[0][0] = 'Chestnut Bun';
$specials[0][1] = 'Walnut Bun';
$specials[0][2] = 'Peanut Bun';
$specials[1][0] = 'Chestnut Salad';
$specials[1][1] = 'Walnut Salad';
// Not including the index adds it to the end of the array
// This creates $specials[1][2]
$specials[1][] = 'Peanut Salad';
print 'The total cost of the dinner is ' . $prices['dinner']['total'] . ".\n";
print 'Special dishes for today are ' . $specials[1][0] . ', ' . $specials[1][1] . ' and ' . $specials[1][2] . '.';
?>
The total cost of the dinner is 21.45. Special dishes for today are Chestnut Salad, Walnut Salad and Peanut Salad.

PHP Example #47

Multi-dimensional arrays and loops

To pass thru each dimension in a multi-dimensional array, use nested foreach() or for() loops. In this example two foreach() loops are used to process a multi-dimensional associative array.

<?php
$flavors = array('Japanese' => array('hot' => 'wasabi',
'salty' => 'soy sauce'),
'Chinese' => array('hot' => 'mustard',
'pepper-salty' => 'prickly ash'));
// $culture is the key and $culture_flavors is the value
foreach ($flavors as $culture => $culture_flavors) {
// $flavor is the key and $example is the value
foreach ($culture_flavors as $flavor => $example) {
print nl2br("A $culture $flavor flavor is $example.\n");
}
}
?>
A Japanese hot flavor is wasabi.
A Japanese salty flavor is soy sauce.
A Chinese hot flavor is mustard.
A Chinese pepper-salty flavor is prickly ash.

PHP Example #48

Multi-dimensional arrays and loops

In this example two for() loops are used to process a multi-dimensional numeric array.

<?php
$specials = array(array('Chestnut Bun','Walnut Bun','Peanut Bun'),
array('Chesnut Salad','Walnut Salad','Peanut Salad'));
// $num_specials is 2: the number of elements in the first dimension of $specials
for ($i = 0, $num_specials = count($specials); $i < $num_specials; $i++) {
// $num_sub is 3: the number of elements in each subarray
for ($m = 0, $num_sub = count($specials[$i]); $m < $num_sub; $m++) {
print nl2br("Element [$i][$m] is " . $specials[$i][$m] . "\n");
}
}
?>
Element [0][0] is Chestnut Bun
Element [0][1] is Walnut Bun
Element [0][2] is Peanut Bun
Element [1][0] is Chesnut Salad
Element [1][1] is Walnut Salad
Element [1][2] is Peanut Salad

PHP Example #49

Multi-dimensional arrays and loops

This is the previous example, but here we interpolate the values of the multi-dimensional arrays in a string enclosed within double quotes to achieve the same result as before, by using the brackets ({}) syntax. Only the print statement is different from the previous example.

<?php
$specials = array(array('Chestnut Bun','Walnut Bun','Peanut Bun'),
array('Chesnut Salad','Walnut Salad','Peanut Salad'));
// $num_specials is 2: the number of elements in the first dimension of $specials
for ($i = 0, $num_specials = count($specials); $i < $num_specials; $i++) {
// $num_sub is 3: the number of elements in each subarray
for ($m = 0, $num_sub = count($specials[$i]); $m < $num_sub; $m++) {
print nl2br("Element [$i][$m] is {$specials[$i][$m]}\n");
}
}
?>
Element [0][0] is Chestnut Bun
Element [0][1] is Walnut Bun
Element [0][2] is Peanut Bun
Element [1][0] is Chesnut Salad
Element [1][1] is Walnut Salad
Element [1][2] is Peanut Salad