|
|
 |
(PHP 4 >= 4.0.0) array_reverse -- Vrátit pole s prvky v opačném pořadí Popisarray array_reverse ( array array [, bool preserve_keys])
array_reverse() takes input
array and returns a new array with the
order of the elements reversed, preserving the keys if
preserve_keys is TRUE.
Příklad 1. array_reverse() example $input = array ("php", 4.0, array ("green", "red"));
$result = array_reverse ($input);
$result_keyed = array_reverse ($input, true); |
|
$result teď obsahuje array (array ("green",
"red"), 4.0, "php"). Ale $result2[0] je stále
"php".
Poznámka:
Druhý argument byl přidán v PHP 4.0.3.
User Contributed Notes array_reverse |
 |
david@audiogalaxy.com
04-Mar-2000 06:40 |
|
With associative arrays array_reverse() keeps key => value pairs matched
but reverses the order of the array as spaned by functions like each().
With numerical indexes array_reverse not only reverses position (as spaned
by each) but also renumbers the keys.
Both cases seem to be what people would generally want: indeed without the
renumbering behavior, someone refering to array elements by numerical key
wouldn't think array_reverse did anything.
However, people who are trying to keep numerical keys associated with their
values - e.g. trying to have holes in their arrays - will be foiled by the
renumbering. The most telling results come from applying array_reverse()
to arrays with mixed keys (some numbers and some strings). The strings
stay attached and the rest of the keys get renumbered around them - most
annoying if you are thinking what you've got is an associative array but
some of your keys happen to be numbers.
|
|
david@audiogalaxy.com
04-Mar-2000 07:39 |
|
As a further clarification: key-value pairs have an order within an array
completely separate from whatever the keys happen to be - the order in
which you add them. This is the order that functions like each() and
next() will move their pointer through the array.
If you add to an array without specifying the key, like $array[] = value;
then an internal counter supplies the key value and then the numerical
order of your keys will be identical to the the internal order. If you
"leave holes" - jumping ahead by specifying a higher number for
the key, like $array[1000] = value; the internal counter gets pushed
forward appropriately. Other than its effect on this internal counter,
specifying a numerical key seems no different than specifying a string.
However, some array functions, like array_merge() and array_reverse() treat
keys that are numbers differently from keys that are not.
|
|
rahulavhad@yahoo.com
30-Dec-2000 02:31 |
|
This code can help in recursive reversing of the array...
$arr1 = array(2,1,array(5,2,1,array(9,8,7)),5,0);
$arr1 = array_reverse($arr1);
function Reverse_Array($array)
{ $index = 0;
foreach ($array as $subarray)
{ if (is_array($subarray))
{ $subarray = array_reverse($subarray);
$arr = Reverse_Array($subarray);
$array[$index] = $arr;
}
else {$array[$index] = $subarray;}
$index++;
}
return $array;
}
$arr2 = Reverse_Array($arr1);
|
|
nospam.nass@btinternet.com.nospam
13-Jun-2001 12:08 |
|
For a similar functionality in PHP3, use something like:
for ($i = count ($foo) - 1 ; $i >= 0; $i-- ) {
do something here
}
... where $foo is the array that you want to reverse.
|
|
bear@datasys.org
07-Jul-2001 08:30 |
|
This code should work, even if you upgrade to a php version that has the
function implemented
if (!function_exists("array_search")) {
function array_search ($needle, $haystack, $strict = FALSE) {
foreach($haystack as $key => $value) {
if ($strict) {
if ($value === $needle) return $key;
} else {
if ($value == $needle) return $key;
}
}
return FALSE;
}
}
|
|
 |
 | |