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:
$capitals = array(
'Alabama' => 'Montgomery',
'Alaska' => 'Juneau',
'Arizona' => 'Phoenix'
);
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:
unset($capitals['California']);
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:
$capitals = array(
'Alabama' => 'Montgomery',
'Alaska' => 'Juneau',
'Arizona' => 'Phoenix'
);
$states = array_flip($capitals);
// $states = array(
// 'Montgomery' => string 'Alabama',
// 'Juneau' => string 'Alaska',
// 'Phoenix' => string 'Arizona'
// );
4. Merging Arrays
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:
$stateCapitals = array(
'Alabama' => 'Montgomery',
'Alaska' => 'Juneau',
'Arizona' => 'Phoenix'
);
$countryCapitals = array (
'Australia' => 'Canberra',
'Austria' => 'Vienna',
'Algeria' => 'Algiers'
);
$capitals = array_merge($stateCapitals, $countryCapitals);
5. Editing Array Values
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:
function capitalize($element)
{
$element = strtolower($element);
return ucwords($element);
}
$capitals = array(
'Alabama' => 'montGoMEry',
'Alaska' => 'Juneau',
'Arizona' => 'phoeniX'
);
$capitals = array_map("capitalize", $capitals);