Chapter 21. Advanced Array Functions

In Chapter 8 we introduced you to arrays, their uses, and some handy functions for working with them. In some subsequent chapters, we saw how PHP returns many of its results as arrays, particular when working with database function sets. This chapter will look at some of the more advanced functions for working with PHP arrays.

Transformations of Arrays

PHP offers a host of functions for manipulating your data once you have it nicely stored in an array. What the functions in this section have in common is that they take your array, do something with it, and return the results in another array. (We will defer the array-sorting functions until a later section.)


Not covered in this chapter are explode() and implode(), which convert strings into arrays and vice versa. We cover these very handy functions in Chapter 22.

In Chapter 8, we incrementally developed a function to print out the entire contents of an array, and in this section we will use the last of these (print_keys_and_values_each()) to show the arrays that are being returned in examples. We'll list this function again here, in a more generic form:

function print_keys_and_values_each($array_to_test) { // reliably prints everything in array reset($array_to_test); while ($array_cell = each($array_to_test)) { $current_value = $array_cell['value']; $current_key = $array_cell['key']; print("Key: ...

Get PHP6 and MySQL® 6 Bible now with O’Reilly online learning.

O’Reilly members experience live online training, plus books, videos, and digital content from 200+ publishers.