note 71396 added to function.array-merge-recursive

From: Date: Sun, 26 Nov 2006 21:18:24 +0000
Subject: note 71396 added to function.array-merge-recursive
Groups: php.notes 
Request: Send a blank email to php-notes+get-120093@lists.php.net to get a copy of this message
I wrote the following for merging arrays, in my project mainly for configuration... Thought someone else might find it usefull. function array_merge_recursive_keys( $first, $second, $greedy=false) { $inter = array_intersect_assoc(array_keys($first), array_keys($second)); # shaired keys # the idea next, is to strip and append from $second into $first foreach ( $inter as $key ) { # recursion if both are arrays if ( is_array($first[$key]) && is_array($second[$key]) ) { $first[$key] = array_merge_recursive_keys($first[$key], $second[$key]); } # non-greedy array merging: else if ( is_array($first[$key] && !$greedy ) ) { $first[$key][] = $second[$key]; } else if ( is_array($second[$key]) && !$greedy ) { $second[$key][] = $first[$key]; $first[$key] = $second[$key]; } # overwrite... else { $first[$key] = $second[$key]; } unset($second[$key]); } # merge the unmatching keys onto first return array_merge($first, $second); } ---- Server IP: 216.194.113.175 Probable Submitter: 71.119.235.161 ---- Manual Page -- http://www.php.net/manual/en/function.array-merge-recursive.php Edit -- https://master.php.net/note/edit/71396 Del: integrated -- https://master.php.net/note/delete/71396/integrated Del: useless -- https://master.php.net/note/delete/71396/useless Del: bad code -- https://master.php.net/note/delete/71396/bad+code Del: spam -- https://master.php.net/note/delete/71396/spam Del: non-english -- https://master.php.net/note/delete/71396/non-english Del: in docs -- https://master.php.net/note/delete/71396/in+docs Del: other reasons-- https://master.php.net/note/delete/71396 Reject -- https://master.php.net/note/reject/71396 Search -- https://master.php.net/manage/user-notes.php

« previous php.notes (#120093) next »