11.07.2015 Views

PHP MySQL - Stilson.net

PHP MySQL - Stilson.net

PHP MySQL - Stilson.net

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

You also want an ePaper? Increase the reach of your titles

YUMPU automatically turns print PDFs into web optimized ePapers that Google loves.

CHAPTER 5 • ARRAYSwww.it-ebooks.infoIf the optional mode parameter is enabled (set to 1), the array will be counted recursively, a featureuseful when counting all elements of a multidimensional array. The first example counts the totalnumber of vegetables found in the $garden array:$garden = array("cabbage", "peppers", "turnips", "carrots");echo count($garden);This returns the following:4The next example counts both the scalar values and array values found in $locations:$locations = array("Italy", "Amsterdam", array("Boston","Des Moines"), "Miami");echo count($locations, 1);This returns the following:6You may be scratching your head at this outcome because there appears to be only five elements inthe array. The array entity holding Boston and Des Moines is counted as an item, just as its contents are.■ Note The sizeof() function is an alias of count(). It is functionally identical.Counting Array Value FrequencyThe array_count_values() function returns an array consisting of associative key/value pairs. Itsprototype follows:array array_count_values(array array)Each key represents a value found in the input_array, and its corresponding value denotes thefrequency of that key’s appearance (as a value) in the input_array. An example follows:$states = array("Ohio", "Iowa", "Arizona", "Iowa", "Ohio");$stateFrequency = array_count_values($states);print_r($stateFrequency);This returns the following:Array ( [Ohio] => 2 [Iowa] => 2 [Arizona] => 1 )118

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

Saved successfully!

Ooh no, something went wrong!