Author Archive

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 […]


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 […]


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 […]


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 […]


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.

Example 1. shuffle() example

<?php
$numbers = range(1, 20);
srand((float)microtime() * 1000000);
shuffle($numbers);
while (list(, $number) = each($numbers)) {
echo “$number “;
}
?>

Note: As of PHP 4.2.0, there is no […]


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.

Example 1. rsort() example

<?php
$fruits = array(“lemon”, “orange”, “banana”, “apple”);
rsort($fruits);
reset($fruits);
while (list($key, $val) = each($fruits)) {
echo “$key = $val\n“;
}
?>

This example would display:

0 = orange
1 = […]


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.

Example 1. reset() example

<?php
$array = array(’step one’, ’step two’, ’step three’, ’step four’);
// by default, […]


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 […]


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 […]


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 […]