|
|
 |
array_values (PHP 4 ) array_values -- Return all the values of an array Descriptionarray array_values ( array input)
array_values() returns all the values from the
input array.
Example 1. array_values() example $array = array ("size" => "XL", "color" => "gold");
print_r(array_values ($array)); |
This will output:
Array
(
[0] => XL
[1] => gold
) |
|
Note:
This function was added to PHP 4, below is an implementation for
those still using PHP 3.
Example 2.
Implementation of array_values() for PHP 3
users
function array_values ($arr) {
$t = array();
while (list($k, $v) = each ($arr)) {
$t[] = $v;
}
return $t;
} |
|
See also array_keys().
User Contributed Notes array_values |
 |
richard@phpguru.org
19-Dec-2001 06:56 |
|
If you have a numerically indexed array with some keys missing, ie 1, 2, 4,
5 and you want to reindex it so it's 1,2,3,4 *without changing the
positions of the values* (ie sort()) then you can use this function to do
it.
|
|
carl at thep.lu.se
29-Jan-2002 09:59 |
|
Indeed you can, and that's what's so great about it. I have, for instance,
a function that returns the results of a database query as an array. I want
to keep the order that the entries were returned in, but at the same time I
want to be able to access them _either_ by the position _or_ by some other
index (such as some sort of ID in the database, gotten from elsewhere). In
this case, I can make the function return an array from id to [array of
values], and by a simple call to array_values() this is transformed into an
array indexed from 0 to count()-1. Useful.
|
|
 |
| |