SakhaliaNetHome PageSahara TerritoryVorKutaAcceptance of cookiesAcceptance of cookies

PHP Tutorial :: Arrays (IV)

PHP Example #43

Arranging associative arrays

To order an associative array by value of element, we can use the asort() function, which will keep together the keys and their values.

<?php
$meal = array('breakfast' => 'Walnut Bun',
'lunch' => 'Cashew Nuts and White Mushrooms',
'snack' => 'Dried Mulberries',
'dinner' => 'Eggplant with Chili Sauce');
print nl2br("Before sorting:\n\n");
foreach($meal as $key => $value) {
print nl2br("\$meal: $key $value\n");
}
print nl2br("\n");
asort($meal);
print nl2br("After sorting:\n\n");
// Now the names of the keys will be kept
foreach($meal as $key => $value) {
print nl2br("\$meal: $key $value\n");
}
?>
Before sorting:

$meal: breakfast Walnut Bun
$meal: lunch Cashew Nuts and White Mushrooms
$meal: snack Dried Mulberries
$meal: dinner Eggplant with Chili Sauce

After sorting:

$meal: lunch Cashew Nuts and White Mushrooms
$meal: snack Dried Mulberries
$meal: dinner Eggplant with Chili Sauce
$meal: breakfast Walnut Bun

PHP Example #44

Arranging associative arrays by key

The ksort() function orders the elements of an array by their key, and like asort() it will keep together the keys and their values.

By using the rsort(), arsort() and krsort() functions, the ordering will be made in inverse order.

<?php
$meal = array('breakfast' => 'Walnut Bun',
'lunch' => 'Cashew Nuts and White Mushrooms',
'snack' => 'Dried Mulberries',
'dinner' => 'Eggplant with Chili Sauce');
print nl2br("Before sorting:\n\n");
foreach($meal as $key => $value) {
print nl2br("\$meal: $key $value\n");
}
print nl2br("\n");
ksort($meal);
print nl2br("After sorting:\n\n");
foreach($meal as $key => $value) {
print nl2br("\$meal: $key $value\n");
}
?>
Before sorting:

$meal: breakfast Walnut Bun
$meal: lunch Cashew Nuts and White Mushrooms
$meal: snack Dried Mulberries
$meal: dinner Eggplant with Chili Sauce

After sorting:

$meal: breakfast Walnut Bun
$meal: dinner Eggplant with Chili Sauce
$meal: lunch Cashew Nuts and White Mushrooms
$meal: snack Dried Mulberries

PHP Example #45

Multi-dimensional arrays

The value of an element of an array can be another array, instead of a simple variable. This is useful when we need to store data that has a more complex structure. We can do this by using the array() statement.

<?php
$meals = array('breakfast' => array('Walnut Bun','Coffee'),
'lunch' => array('Cashew Nuts','White Mushrooms'),
'snack' => array('Dried Mulberries','Salted Sesame Crab'));
$lunches = array(array('Chicken','Eggplant','Rice'),
array('Beef','Scallions','Noodles'),
array('Eggplant','Tofu'));
$flavors = array('Japanese' => array('hot' => 'wasabi',
'salty' => 'soy sauce'),
'Chinese' => array('hot' => 'mustard',
'pepper-salty' => 'prickly ash'));
// Accessing elements in the arrays
print $meals['lunch'][1]; // White Mushrooms
print nl2br("\n");
print $meals['snack'][0]; // Dried Mulberries
print nl2br("\n");
print $lunches[0][0]; // Chicken
print nl2br("\n");
print $lunches[2][1]; // Tofu
print nl2br("\n");
print $flavors['Japanese']['salty']; // Soy sauce
print nl2br("\n");
print $flavors['Chinese']['hot']; // Mustard
?>
White Mushrooms
Dried Mulberries
Chicken
Tofu
soy sauce
mustard