SakhaliaNetHome PageMilitary HistoryVorKutaAcceptance of cookiesAcceptance of cookies

PHP Tutorial :: Arrays (III)

PHP Example #39

Modifying arrays

Elements of an array can be modified as any other independent variable, and like these, they admit any operation performed on them.

<?php
$dishes['Beef Chow Foon'] = 12;
$dishes['Beef Chow Foon']++;
$dishes['Roast Duck'] = 3;
$dishes['total'] = $dishes['Beef Chow Foon'] + $dishes['Roast Duck'];
if ($dishes['total'] > 15) {
print "You ate a lot: ";
}
print 'You ate ' . $dishes['Beef Chow Foon'] . ' dishes of Beef Chow Foon.';
print nl2br("\n\n");
$meals['breakfast'] = 'Walnut Bun';
$meals['lunch'] = 'Eggplant with Chili Sauce';
$amounts = array(3,6);
print "For breakfast, I'd like like $meals[breakfast] and for lunch, ";
print "I'd like $meals[lunch]. I want $amounts[0] at breakfast and ";
print "$amounts[1] at lunch.";
print nl2br("\n\n");
$meals['Walnut Bun'] = '3.95';
$hosts['www.example.com'] = 'web site';
print "A Walnut bun costs {$meals['Walnut Bun']}.\n";
print "www.example.com is a {$hosts['www.example.com']}.";
?>
You ate a lot: You ate 13 dishes of Beef Chow Foon.

For breakfast, I'd like like Walnut Bun and for lunch, I'd like Eggplant with Chili Sauce. I want 3 at breakfast and 6 at lunch.

A Walnut bun costs 3.95. www.example.com is a web site.

PHP Example #40

Removing elements from an array

The unset() statement completely removes an element from an array, this is, not only the value is deleted, but the element completely ceases its existence.

<?php
$dishes['Beef Chow Foon'] = 12;
$dishes['Beef Chow Foon']++;
$dishes['Roast Duck'] = 3;
unset($dishes['Roast Duck']);
$roast_duck = 0;
if (array_key_exists('Roast Duck', $dishes)) {
$roast_duck = 3;
}
$dishes['total'] = $dishes['Beef Chow Foon'] + $roast_duck;
if ($dishes['total'] > 15) {
print "You ate a lot: ";
}
print 'You ate ' . $dishes['Beef Chow Foon'] . ' dishes of Beef Chow Foon.';
?>
You ate 13 dishes of Beef Chow Foon.

PHP Example #41

Turning strings into arrays arrays and viceversa

The explode() function turns a string separated by a certain delimiter into an array, while the implode() function prints all the values of an array, either chained together, separated by a certain delimiter or even enclosed in table cells.

<?php
// Explode() will turn a string separated by a delimiter into an array
$fish = 'Bass, Carp, Pike, Flounder';
$fish_list = explode(', ', $fish);
print "The second fish is $fish_list[1].";
print nl2br("\n\n");
// Implode() prints all the values of an array separated by a delimiter
$dimsum = array('Chicken Bun','Stuffed Duck Web','Turnip Cake');
$menu = implode(', ', $dimsum);
print $menu;
print nl2br("\n\n");
$letters = array('A','B','C','D');
print implode('',$letters);
print nl2br("\n\n");
$dimsum = array('Chicken Bun','Stuffed Duck Web','Turnip Cake');
print '<table summary="Dishes">';
print '<tr><td>' . implode('</td><td>',$dimsum) . '</td></tr>';
print '</table>';
?>
The second fish is Carp.

Chicken Bun, Stuffed Duck Web, Turnip Cake

ABCD

Chicken BunStuffed Duck WebTurnip Cake

PHP Example #42

Arranging arrays

There are several methods for ordering arrays, depending on the type of the array and how we want to order it. The sort() function uses the values of the elements as the criterion for ordering, and should be used only with numeric arrays, because it initializes the keys of the array. The example shows some arrays before and after the sorting.

<?php
$dinner = array('Sweet Corn and Asparagus',
'Lemon Chicken',
'Braised Bamboo Fungus');
$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($dinner as $key => $value) {
print nl2br("\$dinner: $key $value\n");
}
print nl2br("\n");
foreach($meal as $key => $value) {
print nl2br("\$meal: $key $value\n");
}
print nl2br("\n");
sort($dinner);
sort($meal);
print nl2br("After sorting:\n\n");
foreach($dinner as $key => $value) {
print nl2br("\$dinner: $key $value\n");
}
print nl2br("\n");
// The string keys will be replaced with numbers
foreach($meal as $key => $value) {
print nl2br("\$meal: $key $value\n");
}
?>
Before sorting:

$dinner: 0 Sweet Corn and Asparagus
$dinner: 1 Lemon Chicken
$dinner: 2 Braised Bamboo Fungus

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

After sorting:

$dinner: 0 Braised Bamboo Fungus
$dinner: 1 Lemon Chicken
$dinner: 2 Sweet Corn and Asparagus

$meal: 0 Cashew Nuts and White Mushrooms
$meal: 1 Dried Mulberries
$meal: 2 Eggplant with Chili Sauce
$meal: 3 Walnut Bun