note 58829 deleted from function.array-merge by levim
| From: | levim@php.net | Date: | Mon, 24 Feb 2014 22:54:23 +0000 |
| Subject: | note 58829 deleted from function.array-merge by levim | ||
| References: | 1 | Groups: | php.notes |
| Request: | Send a blank email to php-notes+get-198680@lists.php.net to get a copy of this message | ||
Note Submitter: ttsgosadow at gmail dot com
----
I was in need of a version of array_merge to add associative arrays without losing duplicate keys. I
use the following function:
[code]
function array_add( $array1, $array2 )
{
foreach( $array2 AS $key => $value )
{
while( array_key_exists( $key, $array1 ) )
$key .= "_";
$array1[ $key ] = $value;
}
return $array1;
}
[/code]
In my case the $key is a date I want to sort on, and if I happen to have duplicate dates (users add
things on the same timestamp) I lose that entry with array_merge. I can sort on the key without
getting big missorts because of the appended '_'.