unshift a key value pair
| From: | Marc Logghe | Date: | Wed, 09 Jan 2002 08:55:05 +0000 |
| Subject: | unshift a key value pair | ||
| Groups: | php.general | ||
| Request: | Send a blank email to php-general+get-79891@lists.php.net to get a copy of this message | ||
Hi,
I was wondering how you could unshift a key value pair onto an array.
In the manual one suggested using a function like this:
function array_unshift_assoc(&$arr, $key, $val)
{
$arr = array_reverse($arr, true);
$arr[$key] = $val;
$arr = array_reverse($arr, true);
return count($arr);
}
it is a nice trick but this did not really convince me.
When you have eg an array like this: $a = array("foo"=>"bar",
"foo2"=>"bar2") and you do an array_shift operation,
you end up with ("foo2"=>"bar2"). So it is really easy to *remove* a
key-value
pair, but how do you put one in ?
Marc