note 89492 added to function.array-merge

From: Date: Tue, 10 Mar 2009 23:46:05 +0000
Subject: note 89492 added to function.array-merge
Groups: php.notes 
Request: Send a blank email to php-notes+get-151710@lists.php.net to get a copy of this message
The "array_merge_recursive" did not work exactly the way I wanted it to, so I wrote these functions. I have it under "array_merge" simply because I also have a non-recursive version as well. Took me hours to get this right, so hopefully this helps someone else. array array_mix(array $array1 [, array $array2 [, array $... ]]) Merges the elements of one or more arrays together so that the values of one are appended to the end of the previous one. It returns the resulting array. If the new value is not an array then the value gets turned into a new numerically indexed array containing the original value and the new value. Three or more occurrences get appended to the new array. If the new value is an array, then the original value gets overwritten with the new one. This function works exactly the same for both string and numeric based keys. Example: <?php $array_a = array( 'one' => array( 'a1', 'a2', 'a3' ), 'four' => 'one' ); $array_b = array( 'one' => array( 'b1', 'b2', 'b3' ), 'four' => 'two' ); print_r(array_mix($array_a, $array_b, $array_c)); ?> See output. Notice the difference between array_mix and array_merge. array array_mix_recursive(array $array1 [, array $array2 [, array $... ]]) Works exactly the same as "array_mix", with one exception: If the new value is an array then array_mix is done recursively. Example: <?php $array_a = array( 'one' => array( 'a1', 'a2', 'a3' ), 'three' => array( 'i' => 'A1', 'ii' => 'A2', 'iii' => 'A3' ), 'four' => 'one' ); $array_b = array( 'one' => array( 'b1', 'b2', 'b3' ), 'three' => array( 'i' => 'B1', 'ii' => 'B2', 'iii' => array('one', 'two', 'three') ), 'four' => 'two' ); print_r(array_mix_recursive($array_a, $array_b)); ?> See output. This function works very similar to array_merge_recursive, with this obvious exception: Numeric keys are treated just like string keys. CODE: <?php function array_mix() { $array = array(); $arrays = func_get_args(); foreach($arrays as $array_i) if(is_array($array_i)) $array = array_mixer($array, $array_i); return $array; } function array_mix_recursive() { $array = array(); $arrays = func_get_args(); foreach($arrays as $array_i) if(is_array($array_i)) $array = array_mixer($array, $array_i, true); return $array; } function array_mixer($array_o, $array_i, $recursive=false) { foreach($array_i as $k => $v) { if(! isset($array_o[$k])) { $array_o[$k] = $v; } else { if(is_array($array_o[$k])) { if(is_array($v)) { if($recursive) $array_o[$k] = array_mixer($array_o[$k], $v); else $array_o[$k] = $v; } else $array_o[$k][] = $v; } else { if(! isset($array_o[$k])) { $array_o[$k] = $v; } else { $array_o[$k] = array($array_o[$k]); $array_o[$k][] = $v; } } } } return $array_o; } ?> ---- Server IP: 64.71.164.2 Probable Submitter: 74.93.181.202 ---- Manual Page -- http://www.php.net/manual/en/function.array-merge.php Edit -- https://master.php.net/note/edit/89492 Del: integrated -- https://master.php.net/note/delete/89492/integrated Del: useless -- https://master.php.net/note/delete/89492/useless Del: bad code -- https://master.php.net/note/delete/89492/bad+code Del: spam -- https://master.php.net/note/delete/89492/spam Del: non-english -- https://master.php.net/note/delete/89492/non-english Del: in docs -- https://master.php.net/note/delete/89492/in+docs Del: other reasons-- https://master.php.net/note/delete/89492 Reject -- https://master.php.net/note/reject/89492 Search -- https://master.php.net/manage/user-notes.php

« previous php.notes (#151710) next »