Groups of Data: Modifying Arrays in PHP

You can operate on individual array elements just like regular scalar variables, using arithmetic, logical, and other operators. Example 4-18 shows some operations on array elements.

Example 4-18. Operating on array elements

$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.’;

Example 4-18 prints:

You ate a lot: You ate 13 dishes of Beef Chow Foon.

Interpolating array element values in double-quoted strings or here documents is similar to interpolating numbers or strings. The easiest way is to include the array ele­ment in the string, but don’t put quotes around the element key. This is shown in Example 4-19.

Example 4-19. Interpolating array element values in double-quoted strings

$meals[‘breakfast’] = ‘Walnut Bun’;

$meals[‘lunch’] = ‘Eggplant with Chili Sauce’;

$amounts = array(3, 6);


print “For breakfast, I’d like $meals[breakfast] and for lunch,\n”;

print “I’d like $meals[lunch]. I want $amounts[0] at breakfast and\n”;

print “$amounts[1] at lunch.”;

Example 4-19 prints:

For breakfast, I’d like Walnut Bun and for lunch,

I’d like Eggplant with Chili Sauce. I want 3 at breakfast and 6 at lunch.

The interpolation in Example 4-19 works only with array keys that consist exclusively of letters, numbers, and underscores. If you have an array key that has whitespace or other punctuation in it, interpolate it with curly braces, as demonstrated in Example 4-20.

Example 4-20. Interpolating array element values with curly braces

$meals[‘Walnut Bun’] = ‘$3.95’;

$hosts[‘’] = ‘website’;

print “A Walnut Bun costs {$meals[‘Walnut Bun’]}.\n”;

print “ is a {$hosts[‘’]}.”;

Example 4-20 prints:

A Walnut Bun costs $3.95. is a website.

In a double-quoted string or here document, an expression inside curly braces is eval­uated and then its value is put into the string. In Example 4-20, the expressions used are lone array elements, so the element values are interpolated into the strings.

To remove an element from an array, use unset():

unset($dishes[‘Roast Duck’]);

Removing an element with unset() is different than just setting the element value to 0 or the empty string. When you use unset(), the element is no longer there when you iterate through the array or count the number of elements in the array. Using unset() on an array that represents a store’s inventory is like saying that the store no longer carries a product. Setting the element’s value to 0 or the empty string says that the item is temporarily out of stock.

When you want to print all of the values in an array at once, the quickest way is to use the implode() function. It makes a string by combining all the values in an array, putting a string delimiter between each value. Example 4-21 prints a comma- separated list of dim sum choices.

Example 4-21. Making a string from an array with implode()

$dimsum = array(‘Chicken Bun’,’Stuffed Duck Web’,’Turnip Cake’);

$menu = implode(‘, ‘, $dimsum);

print $menu;

Example 4-21 prints:

Chicken Bun, Stuffed Duck Web, Turnip Cake

To implode an array with no delimiter, use the empty string as the first argument to implode():

$letters = array(‘A’,’B’,’C’,’D’);

print implode(”,$letters);

This prints:


Use implode() to simplify printing HTML table rows, as shown in Example 4-22.

Example 4-22. Printing HTML table rows with implode()

$dimsum = array(‘Chicken Bun’,’Stuffed Duck Web’,’Turnip Cake’);

print ‘<tr><td>’ . implode(‘</td><td>’,$dimsum) . ‘</td></tr>’;

Example 4-22 prints:

<tr><td>Chicken Bun</td><td>Stuffed Duck Web</td><td>Turnip Cake</td></tr>

The implode() function puts its delimiter between each value, so to make a complete table row, you also have to print the opening tags that go before the first element and the closing tags that go after the last element.

The counterpart to implode() is called explode(). It breaks a string apart into an array. The delimiter argument to explode() is the string it should look for to separate array elements. Example 4-23 demonstrates explode().

Example 4-23. Turning a string into an array with explode()

$fish = ‘Bass, Carp, Pike, Flounder’;

$fish_list = explode(‘, ‘, $fish);

print “The second fish is $fish_list[1]”;

Example 4-23 prints:

The second fish is Carp

Source: Sklar David (2016), Learning PHP: A Gentle Introduction to the Web’s Most Popular Language, O’Reilly Media; 1st edition.

Leave a Reply

Your email address will not be published. Required fields are marked *