note 58829 added to function.array-merge

From: Date: Thu, 17 Nov 2005 10:17:12 +0000
Subject: note 58829 added to function.array-merge
Groups: php.notes 
Request: Send a blank email to php-notes+get-98695@lists.php.net to get a copy of this message
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 '_'. ---- Manual Page -- http://www.php.net/manual/en/function.array-merge.php Edit -- http://master.php.net/manage/user-notes.php?action=edit+58829 Delete: added to the manual -- http://master.php.net/manage/user-notes.php?action=delete+58829&report=yes&reason=added+to+the+manual Delete: bad code -- http://master.php.net/manage/user-notes.php?action=delete+58829&report=yes&reason=bad+code Delete: spam -- http://master.php.net/manage/user-notes.php?action=delete+58829&report=yes&reason=spam Delete: useless -- http://master.php.net/manage/user-notes.php?action=delete+58829&report=yes&reason=useless Delete: non-english -- http://master.php.net/manage/user-notes.php?action=delete+58829&report=yes&reason=non-english Delete: already in docs -- http://master.php.net/manage/user-notes.php?action=delete+58829&report=yes&reason=already+in+docs Delete: other reasons -- http://master.php.net/manage/user-notes.php?action=delete+58829&report=yes Reject -- http://master.php.net/manage/user-notes.php?action=reject+58829&report=yes Search -- http://master.php.net/manage/user-notes.php

« previous php.notes (#98695) next »