note 96201 added to function.array-merge-recursive

From: Date: Sun, 14 Feb 2010 07:34:48 +0000
Subject: note 96201 added to function.array-merge-recursive
Groups: php.notes 
Request: Send a blank email to php-notes+get-165948@lists.php.net to get a copy of this message
<?php /** * Merges any number of arrays / parameters recursively, replacing * entries with string keys with values from latter arrays. * If the entry or the next value to be assigned is an array, then it * automagically treats both arguments as an array. * Numeric entries are appended, not replaced, but only if they are * unique * * calling: result = array_merge_recursive_distinct(a1, a2, ... aN) **/ function array_merge_recursive_distinct () { $arrays = func_get_args(); $base = array_shift($arrays); if(!is_array($base)) $base = empty($base) ? array() : array($base); foreach($arrays as $append) { if(!is_array($append)) $append = array($append); foreach($append as $key => $value) { if(!array_key_exists($key, $base) and !is_numeric($key)) { $base[$key] = $append[$key]; continue; } if(is_array($value) or is_array($base[$key])) { $base[$key] = array_merge_recursive_distinct($base[$key], $append[$key]); } else if(is_numeric($key)) { if(!in_array($value, $base)) $base[] = $value; } else { $base[$key] = $value; } } } return $base; } ?> ---- Server IP: 69.147.83.197 Probable Submitter: 216.69.163.171 ---- Manual Page -- http://www.php.net/manual/en/function.array-merge-recursive.php Edit -- https://master.php.net/note/edit/96201 Del: integrated -- https://master.php.net/note/delete/96201/integrated Del: useless -- https://master.php.net/note/delete/96201/useless Del: bad code -- https://master.php.net/note/delete/96201/bad+code Del: spam -- https://master.php.net/note/delete/96201/spam Del: non-english -- https://master.php.net/note/delete/96201/non-english Del: in docs -- https://master.php.net/note/delete/96201/in+docs Del: other reasons-- https://master.php.net/note/delete/96201 Reject -- https://master.php.net/note/reject/96201 Search -- https://master.php.net/manage/user-notes.php

« previous php.notes (#165948) next »