#native_company# #native_desc#
#native_cta#

10 PHP Tricks for Associative Array Manipulation Page 2

By W. Jason Gilmore
on December 9, 2010

6. Sorting Arrays by Keys
Flashcard applications often employ a variety of learning techniques involving sorting cards in certain ways, such as alphabetically. You can sort associative arrays by key using the ksort() function:

$capitals = array(
  'Arizona' => 'Phoenix',
  'Alaska'  => 'Juneau',
  'Alabama' => 'Montgomery'
);
ksort($capitals);

Because the array is passed by reference, ksort() will sort the array “in place,” meaning you won’t have to assign the sort results to another variable.
7. Randomizing Array Order
Still another flashcard technique involves randomly sorting the cards. You can easily randomize an array’s entry order using the shuffle() function:

$capitals = array(
  'Arizona' => 'Phoenix',
  'Alaska'  => 'Juneau',
  'Alabama' => 'Montgomery'
);
shuffle($capitals);

If rather than shuffling the array you instead wanted to randomly select a value, check out the array_rand() function.
8. Determining Whether Keys and Values Exist
You can use the in_array() function to determine whether an array element exists:

$capitals = array(
  'Arizona' => 'Phoenix',
  'Alaska'  => 'Juneau',
  'Alabama' => 'Montgomery'
);
if (in_array("Juneau", $capitals))
{
  echo "Exists!";
} else {
  echo "Does not exist!";
}

Lesser known is the ability to also determine whether an array key exists, a feat accomplished with the array_key_exists() function:

$capitals = array(
  'Arizona' => 'Phoenix',
  'Alaska'  => 'Juneau',
  'Alabama' => 'Montgomery'
);
if (array_key_exists("Alaska", $capitals))
{
  echo "Key exists!";
} else {
  echo "Key does not exist!";
}

9. Searching the Array
You might consider making the flashcard learning resource searchable, so users can easily retrieve a state associated with a particular capital. This can be done using the array_search() function:

$capitals = array(
  'Arizona' => 'Phoenix',
  'Alaska'  => 'Juneau',
  'Alabama' => 'Montgomery'
);
$state = array_search('Juneau', $capitals);
// $state = 'Alaska'

10. The Standard PHP Library
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.