SakhaliaNetHome PageMilitary HistoryVorKutaAcceptance of cookiesAcceptance of cookies

PHP Tutorial :: Arrays (I)

PHP Example #30

Creating arrays

Arrays are collections of variables that allow to store multiple values that are interrelated. Each variable (value) stored in an array has assigned an identifier (key) that allows to refer to it; these identifiers can be strings or numbers.

<?php
$vegetables['corn'] = 'Yellow';
$vegetables['beet'] = 'Red';
$vegetables['carrot'] = 'Orange';
print nl2br("$vegetables[corn], $vegetables[beet] and $vegetables[carrot].\n\n");
// An array called $dinner with numeric keys
$dinner[0] = 'Sweet Corn and Asparagus';
$dinner[1] = 'Lemon Chicken';
$dinner[2] = 'Braised Bamboo Fungus';
print nl2br("$dinner[0], $dinner[1] and $dinner[2].\n\n");
// An array called $computers with numeric and string keys
$computers['trs-80'] = 'Radio Shack';
$computers[2600] = 'Atari';
$computers['Adam'] = 'Coleco';
print $computers['trs-80'] . ', ' . $computers[2600] . ' and ' . $computers['Adam'] . '.';
print nl2br("\n\n");
// Creating arrays with the array() statement
$vegetables = array('corn' => 'yellow', 'beet' => 'red', 'carrot' => 'orange');
$dinner = array(0 => 'Sweet Corn and Asparagus', 1 => 'Lemon Chicken', 2 => 'Braised Bamboo Fungus');
$computers = array('trs-80' => 'Radio Shack', 2600 => 'Atari', 'Adam' => 'Coleco');
// Creating a numeric array by specifying only the values; the keys are automatically assigned as numbers.
$dinner = array('Sweet Corn and Asparagus', 'Lemon Chicken', 'Braised Bamboo Fungus');
print nl2br("I want $dinner[0] and $dinner[1].\n\n");
// Creating an array with keys that are automatically increased
// Creating the array $lunch with two elements
// This sets $lunch[0]
$lunch[] = 'Dried Mushrooms in Brown Sauce';
// This sets $lunch[1]
$lunch[] = 'Pineapple and Yu Fungus';
// Creating the array $dinner with three elements
$dinner = array('Sweet Corn and Asparagus', 'Lemon Chicken', 'Braised Bamboo Fungus');
// This sets $dinner[3]
$dinner[] = 'Flank Skin with Spiced Flavor';
// Retrieve the size of an array
$dishes_count = count($dinner);
print nl2br("There are $dishes_count things for dinner.\n\n");
?>
Yellow, Red and Orange.

Sweet Corn and Asparagus, Lemon Chicken and Braised Bamboo Fungus.

Radio Shack, Atari and Coleco.

I want Sweet Corn and Asparagus and Lemon Chicken.

There are 4 things for dinner.

PHP Example #31

Loops and arrays

The elements of an array are usually processed by loops, that execute certain actions for each element found in the array. In this example we create a data table with an array and a foreach() loop.

<?php
$meal = array('breakfast' => 'Walnut Bun',
'lunch' => 'Cashew Nuts and White Mushrooms',
'snack' => 'Dried Mulberries',
'dinner' => 'Eggplant with Chili Sauce');
print '<table summary="Dishes">';
foreach ($meal as $key => $value) {
print "<tr><td>$key</td><td>$value</td></tr>\n";
}
print '</table>';
?>
breakfastWalnut Bun
lunchCashew Nuts and White Mushrooms
snackDried Mulberries
dinnerEggplant with Chili Sauce

PHP Example #32

Loops and arrays

This is an extended version of the previous example. Here we create the same data table but having alternate row colors.

<?php
$row_color = array('red', 'green');
$color_index = 0;
$meal = array('breakfast' => 'Walnut Bun',
'lunch' => 'Cashew Nuts and White Mushrooms',
'snack' => 'Dried Mulberries',
'dinner' => 'Eggplant with Chili Sauce');
print '<table summary="Dishes">';
foreach ($meal as $key => $value) {
print '<tr bgcolor="' . $row_color[$color_index] . '">';
print "<td>$key</td><td>$value</td></tr>\n";
// This alternates $color_index between 0 and 1
$color_index = 1 - $color_index;
}
print '</table>';
?>
breakfastWalnut Bun
lunchCashew Nuts and White Mushrooms
snackDried Mulberries
dinnerEggplant with Chili Sauce

PHP Example #33

Modifying an array

In this example we increase by twice the values of the array. We can't do this by changing the variables of the bucle ($key and $value); we have to use the $key variable as an index inside the array.

<?php
$meals = array('Walnut Bun' => 1,
'Cashew Nuts and White Mushrooms' => 4.95,
'Dried Mulberries' => 3.00,
'Eggplant with Chili Sauce' => 6.50);
foreach ($meals as $dish => $price) {
// $price = $price * 2 doesn't work
$meals[$dish] = $meals[$dish] * 2;
}
// Passes again through the array and prints the changed values
foreach ($meals as $dish => $price) {
printf ("The new price of %s is \$%.2f.\n",$dish ,$price);
}
?>
The new price of Walnut Bun is $2.00. The new price of Cashew Nuts and White Mushrooms is $9.90. The new price of Dried Mulberries is $6.00. The new price of Eggplant with Chili Sauce is $13.00.