The associative array -- an indispensable data type used to describe a collection of unique keys and associated values -- is a mainstay of all programming languages, PHP included. In fact, associative arrays are so central to the task of Web development that PHP supports dozens of functions and other features capable of manipulating array data in every conceivable manner. Such extensive support can be a bit overwhelming to developers seeking the most effective way to manipulate arrays within their applications. In this article, I'll offer 10 tips that can help you shred, slice and dice your data in countless ways.
1. Adding Array Elements
PHP is a weakly typed language, meaning you're not required to explicitly declare an array nor its size. Instead you can both declare and populate the array simultaneously:
Additional array elements can be appended like this:
$capitals['Arkansas'] = 'Little Rock';
If you're dealing with numerically indexed arrays and would rather prepend and append elements using an explicitly-named function, check out the array_push() and array_unshift() functions (these functions don't work with associative arrays).
2. Removing Array Elements
To remove an element from an array, use the unset() function:
When using numerically indexed arrays you have a bit more flexibility in terms of removing array elements in that you can use the array_shift() and array_pop() functions to remove an element from the beginning and end of the array, respectively.
3. Swapping Keys and Values
Suppose you wanted to create a new array called $states, which would use state capitals as the index and state names as the associated value. This task is easily accomplished using the array_flip() function:
Suppose the previous arrays were used in conjunction with a Web-based "flash card" service, and you wanted to provide students with a way to test their knowledge of worldwide capitals, U.S. states included. You can merge arrays containing both state and country capitals using the array_merge() function:
Suppose the data found in an array potentially contains capitalization errors, and you want to correct these errors before inserting the data into the database. You can use the array_map() function to apply a callback to every array element:
The Standard PHP Library (SPL) offers developers with quite a few data structures, iterators, interfaces, exceptions and other features not previously available within the PHP language. Among these features is the ability to iterate over an array using a convenient object-oriented syntax:
$capitals = array(
'Arizona' => 'Phoenix',
'Alaska' => 'Juneau',
'Alabama' => 'Montgomery'
$arrayObject = new ArrayObject($capitals);
foreach ($arrayObject as $state => $capital)
printf("The capital of %s is %s<br />", $state, $capital);
// The capital of Arizona is Phoenix
// The capital of Alaska is Juneau
// The capital of Alabama is Montgomery
This is just one of countless great features bundled into the SPL, be sure to consult the PHP documentation for more information.
About the Author
Jason Gilmore is the founder of the publishing and consulting firm WJGilmore.com. He also is the author of several popular books, including "Easy PHP Websites with the Zend Framework", "Easy PayPal with PHP", and "Beginning PHP and MySQL, Fourth Edition". Follow him on Twitter at @wjgilmore.