11.07.2015 Views

PHP MySQL - Stilson.net

PHP MySQL - Stilson.net

PHP MySQL - Stilson.net

SHOW MORE
SHOW LESS
  • No tags were found...

Create successful ePaper yourself

Turn your PDF publications into a flip-book with our unique Google optimized e-Paper software.

www.it-ebooks.infoCHAPTER 5 • ARRAYSCalculating Associative Array DifferencesThe function array_diff_assoc() operates identically to array_diff(), except that it also considers arraykeys in the comparison. Therefore, only key/value pairs located in the first array but not appearing inany of the other input arrays will be returned in the result array. Its prototype follows:array array_diff_assoc(array array1, array array2 [, array arrayN])The following example only returns "HI" => "Hawaii" because this particular key/value appears in$array1 but doesn’t appear in $array2 or $array3:$array1 = array("OH" => "Ohio", "CA" => "California", "HI" => "Hawaii");$array2 = array("50" => "Hawaii", "CA" => "California", "OH" => "Ohio");$array3 = array("TX" => "Texas", "MD" => "Maryland", "KS" => "Kansas");$diff = array_diff_assoc($array1, $array2, $array3);print_r($diff);This returns the following:Array ( [HI] => Hawaii )■ Tip Introduced in <strong>PHP</strong> 5.0, the array_udiff_assoc(), array_udiff_uassoc(), and array_diff_uassoc()functions are all capable of comparing the differences of arrays in a variety of manners using user-definedfunctions. Consult the <strong>PHP</strong> manual for more information.Other Useful Array FunctionsThis section introduces a number of array functions that perhaps don’t easily fall into one of the priorsections but are no<strong>net</strong>heless quite useful.Returning a Random Set of KeysThe array_rand() function will return a random number of keys found in an array. Its prototype follows:mixed array_rand(array array [, int num_entries])If you omit the optional num_entries parameter, only one random value will be returned. You cantweak the number of returned random values by setting num_entries accordingly. An example follows:$states = array("Ohio" => "Columbus", "Iowa" => "Des Moines","Arizona" => "Phoenix");$randomStates = array_rand($states, 2);print_r($randomStates);131

Hooray! Your file is uploaded and ready to be published.

Saved successfully!

Ooh no, something went wrong!