Using usort in PHP
usort – Sort an array by values using a user-defined comparison function
Description
bool usort ( array array, callback cmp_function)
This function will sort an array by its values using a user-supplied comparison function. If the array you wish to sort needs to be sorted by some non-trivial criteria, you should use this function.
The comparison function must return an integer less than, equal to, or greater than zero if the first argument is considered to be respectively less than, equal to, or greater than the second.
Note: If two members compare as equal, their order in the sorted array is undefined. Up to PHP 4.0.6 the user defined functions would keep the original order for those elements, but with the new sort algorithm introduced with 4.1.0 this is no longer the case as there is no solution to do so in an efficient way.
Returns TRUE on success or FALSE on failure.
This example would display:
|
Note: Obviously in this trivial case the sort() function would be more appropriate.
|
Example 3. usort() example using a member function of an object
This example would display:
|
Using uksort in PHP
uksort – Sort an array by keys using a user-defined comparison function
Description
bool uksort ( array array, callback cmp_function)
uksort() will sort the keys of an array using a user-supplied comparison function. If the array you wish to sort needs to be sorted by some non-trivial criteria, you should use this function.
Function cmp_function should accept two parameters which will be filled by pairs of array keys. The comparison function must return an integer less than, equal to, or greater than zero if the first argument is considered to be respectively less than, equal to, or greater than the second.
Returns TRUE on success or FALSE on failure.
This example would display:
|
Using uasort in PHP
uasort – Sort an array with a user-defined comparison function and maintain index association
Description
bool uasort ( array array, callback cmp_function)
This function sorts an array such that array indices maintain their correlation with the array elements they are associated with. This is used mainly when sorting associative arrays where the actual element order is significant. The comparison function is user-defined.
Returns TRUE on success or FALSE on failure.
Note: Please see usort() and uksort() for examples of user-defined comparison functions.
Using Sort Function in PHP
sort – Sort an array
Description
bool sort ( array array [, int sort_flags])
This function sorts an array. Elements will be arranged from lowest to highest when this function has completed.
Note: This function assigns new keys for the elements in array. It will remove any existing keys you may have assigned, rather than just reordering the keys.
Returns TRUE on success or FALSE on failure.
This example would display:
|
The fruits have been sorted in alphabetical order.
The optional second parameter sort_flags may be used to modify the sorting behavior using these values:
Sorting type flags:
- SORT_REGULAR - compare items normally
- SORT_NUMERIC - compare items numerically
- SORT_STRING - compare items as strings
Note: The second parameter was added in PHP 4.
Using shuffle in PHP
shuffle – Shuffle an array
Description
void shuffle ( array array)
This function shuffles (randomizes the order of the elements in) an array. You must use srand() to seed this function.
|
Note: As of PHP 4.2.0, there is no need to seed the random number generator with srand() or mt_srand() as this is now done automatically.
Using rsort in PHP
rsort – Sort an array in reverse order
Description
bool rsort ( array array [, int sort_flags])
This function sorts an array in reverse order (highest to lowest).
Returns TRUE on success or FALSE on failure.
This example would display:
|
The fruits have been sorted in reverse alphabetical order.
You may modify the behavior of the sort using the optional parameter sort_flags
Using reset in PHP
reset – Set the internal pointer of an array to its first element
Description
mixed reset ( array array)
reset() rewinds array’s internal pointer to the first element and returns the value of the first array element.
|
Using range function in PHP
range – Create an array containing a range of elements
Description
array range ( int low, int high [, int step])
range() returns an array of elements from low to high, inclusive. If low > high, the sequence will be from high to low.
New parameter: The optional step parameter was added in 5.0.0.
If a step value is given, it will be used as the increment between elements in the sequence. step should be given as a positive number. If not specified, step will default to 1.
|
Note: Prior to PHP version 4.1.0, range() only generated incrementing integer arrays. Support for character sequences and decrementing arrays was added in 4.1.0. Character sequence values are limited to a length of one. If a length greater than one is entered, only the first character is used.
| Caution |
| In PHP versions 4.1.0 through 4.3.2, range() sees numeric strings as strings and not integers. Instead, they will be used for character sequences. For example, “4242″ is treated as “4″. |
Using Prev in PHP
prev – Rewind the internal array pointer
Description
mixed prev ( array array)
Returns the array value in the previous place that’s pointed to by the internal array pointer, or FALSE if there are no more elements.
| Warning |
| If the array contains empty elements then this function will return FALSE for these elements as well. To properly traverse an array which may contain empty elements see the each() function. |
prev() behaves just like next(), except it rewinds the internal array pointer one place instead of advancing it.
|
Example 1. Example use of prev() and friends
|
Using Next Function in PHP
next – Advance the internal array pointer of an array
Description
mixed next ( array array)
Returns the array value in the next place that’s pointed to by the internal array pointer, or FALSE if there are no more elements.
next() behaves like current(), with one difference. It advances the internal array pointer one place forward before returning the element value. That means it returns the next array value and advances the internal array pointer by one. If advancing the internal array pointer results in going beyond the end of the element list, next() returns FALSE.
| Warning |
| If the array contains empty elements, or elements that have a key value of 0 then this function will return FALSE for these elements as well. To properly traverse an array which may contain empty elements or elements with key values of 0 see the each() function. |
|
Example 1. Example use of next() and friends
|
