- Accessing All Elements of Numeric Arrays
- Accessing All Elements of Associative Arrays
- Accessing All Array Elements in Nested Arrays
- Turning an Array into Variables
- Converting Strings to Arrays
- Converting Arrays to Strings
- Sorting Arrays Alphabetically
- Sorting Associative Arrays Alphabetically
- Sorting Nested Arrays
- Sorting Nested Associative Arrays
- Sorting IP Addresses (as a Human Would)
- Sorting Anything
- Sorting with Foreign Languages
- Applying an Effect to All Array Elements
- Filtering Arrays
- Getting Random Elements Out of Arrays
- Making Objects Behave Like Arrays
Applying an Effect to All Array Elements
$a = array_map('sanitize', $a);
<?php function sanitize($s) { return htmlspecialchars($s); } $a = array('harmless', '<bad>', '>>click here!<<'); $a = array_map('sanitize', $a); echo implode(' ', $a); ?>
Applying htmlspecialchars() to All Elements of an Array (array_map.php)
Sometimes, data in an array has to be preprocessed before it can be used. In Chapter 4, you will see how data coming from the user via HTML forms can be sanitized before it is used. To do so, every array element must be touched.
However, it is not required that you do a cumbersome for/foreach/while loop; PHP offers built-in functionality for this. The first possibility is to use array_map(). This takes an array (second parameter) and submits every element in that array to a callback function (first parameter, as a string, an array, or an anonymous function, as you can see below). At the end, the array is returned, with all of the elements replaced by the associated return values of the callback function.
In the preceding listing, all values in the array are converted into HTML using htmlspecialchars().
<?php $a = array('harmless', '<bad>', '>>click here!<<'); $a = array_map( function sanitize($s) { return htmlspecialchars($s); }. $a); echo implode(' ', $a); ?>
Applying htmlspecialchars() to All Elements of an Array, Using an Anonymous Function (array_map_anon.php)
If the array turns out to be a nested one, however, the tactic has to be changed a little. Then you can use a recursive function. If an array element is a string, it is HTML encoded. If it’s an array, the recursive function calls itself on that array. The following code implements this, and Figure 2.6 shows the result:
<?php function sanitize_recursive($s) { if (is_array($s)) { return(array_map('sanitize_recursive', $s)); } else { return htmlspecialchars($s); } } $a = array( 'harmless' => array('no', 'problem'), 'harmful' => array('<bad>', '—> <worse> <<-') ); $a = sanitize_recursive($a); echo '<pre>' . print_r($a, true) . '</pre>'; ?>
Recursively Applying htmlspecialchars() to All Elements of a Nested Array (array_map_recursive.php)
Figure 2.6. The nested arrays have been HTML-encoded.
Another function that behaves similarly is array_walk(). This one also applies a function to every element of an array; however, it also allows you to provide a parameter for this function call. In the following code, this is used to print out all elements of an array. A parameter is passed—a counter. Because it is passed by reference, increasing this counter by one within the function leads to a sequence of numbers:
<?php function printElement($s, &$i) { printf('%d: %s<br />', $i, htmlspecialchars($s)); $i++; } $i = 1; $a = array('one', 'two', 'three', 'four'); $a = array_walk($a, 'printElement', $i); ?>
Printing Array Elements Using array_walk() and a Counter Passed by Reference (array_walk.php)
Running the preceding code shows the following:
0: one 1: two 2: three 3: four