note 92521 deleted from function.array-unshift by danbrown
| From: | danbrown@php.net | Date: | Wed, 12 Aug 2009 16:38:45 +0000 |
| Subject: | note 92521 deleted from function.array-unshift by danbrown | ||
| References: | 1 | Groups: | php.notes |
| Request: | Send a blank email to php-notes+get-159377@lists.php.net to get a copy of this message | ||
Note Submitter: nicky dot demaeyer at gmail dot com
----
Small addition to the assoc unshift below: only add if the key passed is unique
<?php
//adds a key/value pair to the beginning of an array without resetting numeric keys
function array_unshift_assoc( &$array, $key, $value, $overwrite_key = false )
{
//if the key can't overwrite and allready exists return false
if (!$overwrite_key && array_key_exists( $key, $array )) return false;
array_reverse( $array, true );
$array[$key] = $value;
array_reverse( $array, true );
return count( $array );
}
?>