Working with arrays in PHP (part ii)
Sunday, September 10th, 2006This article is the second part of articles series on arrays usage in PHP. It covers usage of predefined sorting, searching and filtering functions. There are plenty of functions for sorting, searching and filtering arrays in PHP. This article will help you with choosing the appropriate function to use in a given problem.
Using sort(), asort(), arsort(), rsort(), ksort() functions.
All these functions take two parameters, the second one is optional. They are used for sorting the array elements. The first parameter is the array to be sorted. The second parameter can take three values:
SORT_REGULAR (compare items normally),
SORT_NUMERIC (compare items numerically),
SORT_STRING (compare items as strings).
sort() function sorts specified array by its elements according to the sorting type specified.
rsort() acts like sort() but it does sorting in reverse order.
asort() function sorts the specified array by elements values and changes their order but it doesn't reassign values with indexes, it is mostly important for arrays with string indexes.
arsort() is similar to asort() except that it sorts the array in reverse order.
Here is an example that shows differences between all these sorting functions:
The output will be the following:
-
Initial content: Array ( [0] => red [1] => green [2] => blue )
-
Sorted with sort(): Array ( [0] => blue [1] => green [2] => red )
-
Sorted with rsort(): Array ( [0] => red [1] => green [2] => blue )
-
Sorted with asort(): Array ( [2] => blue [1] => green [0] => red )
-
Sorted with arsort(): Array ( [0] => red [1] => green [2] => blue )
As you can see, asort() and arsort() functions don't change indexes but change the actual items order.
ksort() and krsort() functions.
These functions are very similar with the above except one detail. They sort an array by its keys values, instead of elements values. This is maily useful for string-indexed arrays. Here is an example code what can explain how this works:
As you can see array items are sorted by keys and not by values:
-
Sorted with ksort(): Array ( [a] => One => Two [c] => Three [d] => Four )
-
Sorted with krsort(): Array ( [d] => Four [c] => Three [b] => Two [a] => One )
Using "natural sorting".
"Natural sorting" means sorting more close to human than traditional computer sorting algorithms. PHP offeres two functions for natural sorting: natsort() and natcasesort() where the first is case-sensitive and the second case-insensitive. To understand how "natural sorting" works look at the following example:
This will give the following output:
-
Array ( [0] => image1.png [2] => image2.png [1] => image10.png [3] => image20.png )
This result is unlike the traditional algorithm what will give result like following: (image1, image10, image2, image20).
Searching for items in array.
PHP has two functions for searching items in arrays. They are: in_array() and array_search(). They both search for a needle(first argument) in haystack(second argument). They both take two arguments; the first is the element to be found and the last is optional and means the strictness of search. If it is set to TRUE it checks not only needle and haystack's elements equality but also that they both are the same type. If found, in_array() returns TRUE, else it returns FALSE. The array_search() returns the index of seeking value in specified array. Here is an example of these functions usage:
It will output:
-
Found on index 4
Filtering array.
PHP has a filter() function that allows to filter an array with an own callback function. It takes two arguments: array and callback function. The function returns the filtered array. Here is the code that shows this:
-
function cms_ood($value)
-
{
-
return ($value%2!=1);
-
}
-
function cms_even($value)
-
{
-
return ($value%2!=0)
-
}
Here is the result of the script:
-
Filtered odd: Array ( [1] => 2 [3] => 4 )
-
Filtered even: Array ( [0] => 1 [2] => 3 [4] => 5 )
Now you know most of PHP's array sorting, searching and filtering features and can decide what function would be better for your task.
