Prev | Current Page 214 | Next

W. Jason Gilmore

"Beginning PHP and MySQL: From Novice to Professional"

It is functionally identical.
Counting Array Value Frequency
The array_count_values() function returns an array consisting of associative key/value
pairs. Its prototype follows:
array array_count_values(array array)
Each key represents a value found in the input_array, and its corresponding value
denotes the frequency 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);
CHAPTER 5 ?–  ARRAYS 145
This returns the following:
Array ( [Ohio] => 2 [Iowa] => 2 [Arizona] => 1 )
Determining Unique Array Values
The array_unique() function removes all duplicate values found in an array, returning an
array consisting of solely unique values. Its prototype follows:
array array_unique(array array)
An example follows:
$states = array("Ohio","Iowa","Arizona","Iowa","Ohio");
$uniqueStates = array_unique($states);
print_r($uniqueStates);
This returns the following:
Array ( [0] => Ohio [1] => Iowa [2] => Arizona )
Sorting Arrays
To be sure, data sorting is a central topic of computer science. Anybody who??™s taken
an entry-level programming class is well aware of sorting algorithms such as bubble,
heap, shell, and quick. This subject rears its head so often during daily programming
tasks that the process of sorting data is as common as creating an if conditional or a
while loop.


Pages:
202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226