note 34472 deleted from function.array-combine by sniper
| From: | sniper@php.net | Date: | Mon, 06 Oct 2003 22:57:24 +0000 |
| Subject: | note 34472 deleted from function.array-combine by sniper | ||
| References: | 1 | Groups: | php.notes |
| Request: | Send a blank email to php-notes+get-57932@lists.php.net to get a copy of this message | ||
Note Submitter: edvvard.at.hotmail.dot.com
----
I know that others have already posted one, but here's yet another pre-PHP5 array_combine. This
is safe for associatively keyed arrays and has the same requirements as the PHP5 version: neither
array can be empty and both must be the same size.
In the same style as the PHP function definitions, use the function like this:
array = array_combine ($keys, $values)
Where $keys is an array whose values you want to use as the keys for the combined array, and $values
is an array whose values you want to use as the values for the combined array. It returns the
combined array on success, and FALSE otherwise.
As well, note that the variables are passed to the function by reference. Because of the use of
array_values() to overwrite the keys in both of the arrays that you pass to the function, any array
you pass to the function that has associative keys will LOSE those keys after the function is
called. You'll have to pass the arrays by value if you don't want that behaviour.
function array_combine (&$keys, &$values)
{
if ( (count($keys) === count($values)) && (count($keys) > 0 &&
count($values) > 0) )
{
$keys = array_values($keys);
$values = array_values($values);
for (@$i = 0; $i < count($values); $i++)
{
@$return[$keys[$i]] =& $values[$i];
}
return $return;
}
else
{
return false;
}
}