(PHP 4, PHP 5)
array_merge — Führt ein oder mehrere Arrays zusammen
array_merge() fügt die Elemente von einem oder mehreren Arrays zusammen, indem die Werte des einen an das Ende des anderen angehängt werden. Das daraus resultierende Array wird zurückgegeben.
Haben die angegebenen Arrays die selben String-Schlüssel, dann wird der hintere Wert dieses Schlüssels den vorhergehenden überschreiben. Haben die Arrays den gleichen numerischen Schlüssel, dann wird der hintere Wert den vorhergehenden nicht überschreiben, sondern angehängt.
Falls nur ein Array angegeben und dieses numerisch indiziert ist, wird das Array fortlaufend neu nummeriert.
Beispiel #1 array_merge()
<?php
$array1 = array("farbe" => "rot", 2, 4);
$array2 = array("a", "b", "farbe" => "grün", "form" => "trapezoid", 4);
$result = array_merge($array1, $array2);
print_r($result);
?>
Nun ist $result:
Array ( [farbe] => grün [0] => 2 [1] => 4 [2] => a [3] => b [form] => trapezoid [4] => 4 )
Beispiel #2 Einfaches array_merge() Beispiel
<?php
$array1 = array();
$array2 = array(1 => "data");
$result = array_merge($array1, $array2);
?>
Vergessen Sie nicht, dass numerische Schlüssel neu numeriert werden!
Array ( [0] => data )
Wenn Sie die Arrays komplett erhalten und nur eines an das andere anhängen wollen, verwenden Sie den Operator +:
<?php
$array1 = array();
$array2 = array(1 => "data");
$result = $array1 + $array2;
?>
Der numerische Schlüssel sowie die Assoziation bleiben erhalten.
Array ( [1] => data )
Das Verhalten von array_merge() wurde in PHP 5 geändert. Anders als in PHP 4 akzeptiert array_merge() jetzt nur noch Parameter vom Typ array. Man kann jedoch Typumwandlung verwenden, um andere Typen zusammenzuführen. Schauen Sie für Details in das Beispiel unten.
Beispiel #3 array_merge() PHP 5 Beispiel
<?php
$beginning = 'foo';
$end = array(1 => 'bar');
$result = array_merge((array)$beginning, (array)$end);
print_r($result);
?>
Das oben gezeigte Beispiel erzeugt folgende Ausgabe:
Array ( [0] => foo [1] => bar )
Siehe auch array_merge_recursive(), array_combine() und array operators.
WARNING: numeric subindexes are lost when merging arrays.
Check this example:
$a=array('abc'=>'abc','def'=>'def','123'=>'123','xyz'=>'xyz');
echo "a=";print_r($a);
$b=array('xxx'=>'xxx');
echo "b=";print_r($b);
$c=array_merge($a,$b);
echo "c=";print_r($c);
The result is this:
c=Array
(
[abc] => abc
[def] => def
[0] => 123
[xyz] => xyz
[xxx] => xxx
)
On line to merge arrays WITHOUT REINDEXING numerical keys and WITH OVERWRITING:
$result = array_diff_key($a, $b) + $b
<?php
// example
$array1 = array(0 => 'zero_a', 2 => 'two_a', 3 => 'three_a');
$array2 = array(1 => 'one_b', 3 => 'three_b', 4 => 'four_b');
$result = array_diff_key($array1, $array2) + $array2;
//$result = array(
// 0 => 'zero_a'
// 2 => 'two_a'
// 1 => 'one_b'
// 3 => 'three_b'
// 4 => 'four_b')
?>
Uniques values merge for array_merge function:
<?php
function array_unique_merge() {
$variables = '$_'.implode(',$_',array_keys(func_get_args()));
$func = create_function('$tab', ' list('.$variables.') = $tab; return array_unique(array_merge('.$variables.'));');
return $func(func_get_args());
}
?>
Doesn't work on objects.
Smarter way for uniques values merge array:
<?php
function array_unique_merge() {
return array_unique(call_user_func_array('array_merge', func_get_args()));
}
?>
Example 1:
$a = array(1,2);
$b = array(2,3,4);
array_unique_merge($a,$b);
Return:
array(1,2,3,4);
Example 2:
$a = array(1,2);
$b = array(2,3,4);
$c = array(2,3,4,5);
array_unique_merge($a,$b);
Return:
array(1,2,3,4,5);
Doesn't work on objects.
I keep seeing posts for people looking for a function to replace numeric keys.
No function is required for this, it is default behavior if the + operator:
<?php
$a=array(1=>"one", "two"=>2);
$b=array(1=>"two", "two"=>1, 3=>"three", "four"=>4);
print_r($a+$b);
?>
Array
(
[1] => one
[two] => 2
[3] => three
[four] => 4
)
How this works:
The + operator only adds unique keys to the resulting array. By making the replacements the first argument, they naturally always replace the keys from the second argument, numeric or not! =)
I always confuse which does what and when. So I made this cheatsheet that you may find useful.
The existing examples show some situations but not all of these four.
<?php
$n1 = array('foo', 'bar', 'baz');
$n2 = array('lorem', 'ipsum', 'foo');
// array plus:
// - duplicate numeral keys are ignored, not overwriting
// - new keys inserted at the end
$n1 + $n2;
?>
array(3) {
[0]=>
string(3) "foo"
[1]=>
string(3) "bar"
[2]=>
string(3) "baz"
}
<?php
// array_merge:
// - duplicate numeral keys are appended and keys are reindexed
// - new keys inserted at the end
array_merge( $n1, $n2 );
?>
array(6) {
[0]=>
string(3) "foo"
[1]=>
string(3) "bar"
[2]=>
string(3) "baz"
[3]=>
string(5) "lorem"
[4]=>
string(5) "ipsum"
[5]=>
string(3) "foo"
}
<?php
$a1 = array('f' => 'foo', 'b' => 'bar', 'q' => 'quux');
$a2 = array('l' => 'lorem', 'i' => 'ipsum', 'f' => 'facilisis');
// array plus:
// - duplicate associative array keys ignored, not overwriting
// - new keys inserted at the end
$a1 + $a2;
?>
array(5) {
["f"]=>
string(3) "foo"
["b"]=>
string(3) "bar"
["q"]=>
string(4) "quux"
["l"]=>
string(5) "lorem"
["i"]=>
string(5) "ipsum"
}
<?php
// array plus:
// - replacements are done at the original position
// - new keys inserted at the end
array_merge( $a1, $a2 )
?>
array(5) {
["f"]=>
string(9) "facilisis"
["b"]=>
string(3) "bar"
["q"]=>
string(4) "quux"
["l"]=>
string(5) "lorem"
["i"]=>
string(5) "ipsum"
}
I'm sorry, but I've not found on this page a function that would properly merge two arrays while overwriting numeric values.
Considering two arrays:
<?php
$test1 = array(array('a','b','c'), 'a', 'b');
$test2 = array(array('d','e'), 'c');
?>
while using overwrite merge you would expect the result to be an array:
<?php $test3 = array(array('d','e'), 'c', 'b'); ?>
However most of the functions will result with this array:
<?php $test4 = array(array('d','e','c'), 'c', 'b'); ?>
So, here's a function to do that:
<?php
function array_merge_overwrite(array &$array1, array $array2 = NULL)
{
$flag = true;
foreach (array_keys($array2) as $key)
{
if (isset($array2[$key]) && is_array($array2[$key]))
{
if (isset($array1[$key]) && is_array($array1[$key]))
array_merge_overwrite($array1[$key], $array2[$key]);
else
$array1[$key] = $array2[$key];
$flag = false;
}
}
if ($flag == true)
$array1 = $array2;
}
?>
If you need to add the same array N times (with the same keys, of course) to an existing array, array_merge() will not work because of the same keys. The plus operator (+) still doesn't resolve the problem because of the keys.
Example:
<?php
$args = array();
for($i=0; $i < 3; $i++) {
$args += array('first', 'second', 'three', 'four');
}
print_r($args);
?>
Will give you:
array(
[0] => 'first'
[1] => 'second'
[2] => 'three'
[3] => 'four'
)
The solution is to play with the first key of the array you are adding:
<?php
$args = array();
for($i=0; $i < 3; $i++) {
$args += array(sizeof($args)+1 => 'first', 'second', 'three', 'four');
}
print_r($args);
?>
Now it is added as expected:
array(
[0] => 'first'
[1] => 'second'
[2] => 'three'
[3] => 'four'
[4] => 'first'
[5] => 'second'
[6] => 'three'
[7] => 'four'
[8] => 'first'
[9] => 'second'
[10] => 'three'
[11] => 'four'
)
Reiterating the notes about casting to arrays, be sure to cast if one of the arrays might be null:
<?php
header("Content-type:text/plain");
$a = array('zzzz', 'xxxx');
$b = array('mmmm','nnnn');
echo "1 ==============\r\n";
print_r(array_merge($a, $b));
echo "2 ==============\r\n";
$b = array();
print_r(array_merge($a, $b));
echo "3 ==============\r\n";
$b = null;
print_r(array_merge($a, $b));
echo "4 ==============\r\n";
$b = null;
print_r(array_merge($a, (array)$b));
echo "5 ==============\r\n";
echo is_null(array_merge($a, $b)) ? 'Result is null' : 'Result is not null';
?>
Produces:
1 ==============
Array
(
[0] => zzzz
[1] => xxxx
[2] => mmmm
[3] => nnnn
)
2 ==============
Array
(
[0] => zzzz
[1] => xxxx
)
3 ==============
4 ==============
Array
(
[0] => zzzz
[1] => xxxx
)
5 ==============
Result is null
Be ready to surprise like this one.
array_merge renumbers numeric keys even if key is as string.
keys '1' & '2' will be updated to 0 & 1, but '1.1' & '1.2' remain the same, but they are numeric too (is_numeric('1.1') -> true)
It's better to use '+' operator or to have your own implementation for array_merge.
<?php
$x1 = array (
'1' => 'Value 1',
'1.1' => 'Value 1.1',
);
$x2 = array (
'2' => 'Value 2',
'2.1' => 'Value 2.1',
);
$x3 = array_merge( $x1, $x2 );
echo '<pre>NOT as expected: '. print_r( $x3, true ) .'</pre>';
$x3 = $x1 + $x2;
echo '<pre>As expected: '. print_r( $x3, true ) .'</pre>';
?>
NOT as expected: Array
(
[0] => Value 1
[1.1] => Value 1.1
[1] => Value 2
[2.1] => Value 2.1
)
As expected: Array
(
[1] => Value 1
[1.1] => Value 1.1
[2] => Value 2
[2.1] => Value 2.1
)
Note that if you use + to merge array in order to preserve keys, that in case of duplicates the values from the left array in the addition is used.
I had a hard time using array_merge with large datasets. By the time my web framework was in memory there wasn't enough space to have multiple copies of my dataset. To fix this I had to remove any functions which operated on the data set and made a copy in memory (pass by value).
I realize the available memory to an application instance is modifiable, but I didn't think I should have to set it below the default 16mb for a web app!
Unfortunately, a number of php functions pass by value internally, so I had to write my own merge function. This passes by reference, utilizes a fast while loop (thus doesn't need to call count() to get an upper boundary, also a php pass by value culprit), and unsets the copy array (freeing memory as it goes).
<?php
function mergeArrays(&$sourceArray, &$copyArray){
//merge copy array into source array
$i = 0;
while (isset($copyArray[$i])){
$sourceArray[] = $copyArray[$i];
unset($copyArray[$i]);
$i++;
}
}
?>
This fixed the problem. I would love to know if there is an even faster, more efficient way. Simply adding the two arrays won't work since there is an overlap of index.
This function merges two arrays into one, alternating between the two arrays. Given:
$one = array('apple', 'pear');
$two = array('blue', 'red');
$output = array();
array_merge_zipper(&$output, $one, $two, TRUE);
$output would be:
array(
0 => 'apple',
1 => 'blue',
2 => 'pear',
3 => 'red'
);
The $same_size flag is to note whether both input arrays contain the same number of items. If not, setting the flag to FALSE will make the function merge up to the number of items in the smallest of the two input arrays.
<?php
function array_merge_zipper(&$output, $one = array(), $two = array(), $same_size= TRUE) {
if ($same_size) {
// Return FALSE if the arrays differ in size.
if (count($one) != count($two))
return FALSE;
$number = count($one);
} else {
// Count as high as the smallest array.
$number = min(count($one), count($two));
}
// Empty the output array.
$output = array();
// Merge.
for ($i = 0; $i < $number; $i++) {
$output[] = $one[$i];
$output[] = $two[$i];
}
// Return success.
return TRUE;
}
?>
The following function does essentially the same, but preserves array keys. It'll return FALSE if the input arrays share a key between them or if the arrays differ in size.
<?php
function array_merge_zipper_key(&$output, $one = array(), $two = array()) {
// Return FALSE if the arrays differ in size.
if (count($one) != count($two))
return FALSE;
// Returns FALSE if the arrays share a key.
if (count(array_keys_intersect($one, $two)))
return FALSE;
// Empty the output array.
$output = array();
// keys!
$one_keys = array_keys($one);
$two_keys = array_keys($two);
// Merge.
for ($i = 0; $i < count($one); $i++) {
$output[$one_keys[$i]] = $one[$one_keys[$i]];
$output[$two_keys[$i]] = $two[$two_keys[$i]];
}
// Return success.
return TRUE;
}
be aware there is a slight difference:
<?php
$array1 = array('lang'=>'js','method'=>'GET');
$array2 = array('lang'=>'php','browser'=>'opera','method'=>'POST');
?>
BETWEEN:
<?php array_merge($array2,$array1); ?>
outputs: Array ( [lang] => js [browser] => opera [method] => GET )
notice that the repeated keys will be overwritten by the ones of $array1, maintaining those, because it is the array that is being merged
AND
<?php array_merge($array1,$array2); ?>
outputs: Array ( [lang] => php [method] => POST [browser] => opera )
here the oposite takes place: array1 will have its elements replaced by those of array2.
Apparently there is a shorthand that works very much like array_merge. I figured I would leave a comment here, sense I saw it used in code but found nothing about it from google searches.
<?php
$array = array(
'name' => 'John Doe'
);
$array += array(
'address' => 'someplace somestreet'
);
echo $array['address']; // Outputs 'someplace somestreet'
?>
A difference from merge_array, it does NOT overwrite existing keys, regardless of key type.
"Extend" recursively array $a with array $b values (no deletion in $a, just added and updated values)
<?php
function array_extend($a, $b) {
foreach($b as $k=>$v) {
if( is_array($v) ) {
if( !isset($a[$k]) ) {
$a[$k] = $v;
} else {
$a[$k] = array_extend($a[$k], $v);
}
} else {
$a[$k] = $v;
}
}
return $a;
}
?>
I am in the middle of recoding a very complex CMS I wrote in the past, and I often used array_merge() to determine default values (example: paths, keys which refer to objects elsewhere, etc).
I was trying to decide if I should use array_merge or individual calls to isset in combination with an $opts array, to supply default values in functions. For example, here would be two simple examples:
<?php
function foo( array $opts = array() ) {
if ( isset($opts['val1']) ) $opts['val1'] = 'value';
if ( isset($opts['val2']) ) $opts['val2'] = 'value';
if ( isset($opts['val3']) ) $opts['val3'] = 'value';
# ... continue coding
}
function bar( array $opts = array() ) {
$opts = array_merge( array(
'val1' => 'value',
'val2' => 'value',
'val3' => 'value',
), $opts);
# ... continue coding
}
?>
Hypothesis: isset() will be dramatically faster in both circumstances, as benchmarks I have performed in the past usually demonstrate that array iterations are typically slow and should be avoided when and where possible. (ex: using strrpos and substr rather than explode and array_pop to get a file name from a path, of course you can just use basename() but this is just for explanation purposes)
---
I performed numerous benchmarks using loops, and then hard-coded examples and got fairly similar results. My four focus points however, were supplying the values in the $opts array, and not supplying the values, with both methods.
Calls to isset() WITH the values supplied was ALWAYS the fastest (which makes sense, as no assignments are made)
Calls to isset() WITH the values supplied, and array_merge() with NO values set came very close in times, and fluctuated which was faster or slower...
(This surprised me as I expected that working with array_merge would simply take more time; But also makes sense as I think about it more as you are still utilizing an array and array_merge is probably making isset() calls in itself)
Calls to array_merge() with NO values supplied was always the SLOWEST. (which also makes sense, because all values are replaced)
---
Conclusion: Calls to isset() are faster than using array_merge(); But not by much. However, the more values supplied when calling the function determine how much closer the execution time will take. Therefor, with this type of functionality in mind, supplying DEFAULT values mean often NO, SOME, or ALL values will be included when calling the function...
Because I am worried about the execution time over easy / less code, I'll stick with the isset() method of supplying default values to an array.
---
Now that the test results are in, I thought I would share as I spent quite a bit of time testing and going over results.
More on the union (+) operator:
the order of arrays is important and does not agree in my test below
with the other posts. The 'unique' operation preserves the initial key-value and discards later duplicates.
PHP 5.2.6-2ubuntu4.2
<?php
$a1=array('12345'=>'a', '23456'=>'b', '34567'=>'c', '45678'=>'d');
$a2=array('34567'=>'X');
$a3=$a1 + $a2;
$a4=$a2 + $a1;
print('a1:'); print_r($a1);
print('a2:'); print_r($a2);
print('a3:'); print_r($a3);
print('a4:'); print_r($a4);
?>
a1:Array
(
[12345] => a
[23456] => b
[34567] => c
[45678] => d
)
a2:Array
(
[34567] => X
)
a3:Array
(
[12345] => a
[23456] => b
[34567] => c
[45678] => d
)
a4:Array
(
[34567] => X
[12345] => a
[23456] => b
[45678] => d
)
array_merge will merge numeric keys in array iteration order, not in increasing numeric order. For example:
<?php
$a = array(0=>10, 1=>20); // same as array(10, 20);
$b = array(0=>30, 2=>50, 1=>40);
?>
array_merge($a, $b) will be array(10, 20, 30, 50, 40) and not array(10, 20, 30, 40, 50).
More about the union operator (+)...
The "array_unique" that gets applied, is actually based on the keys, not the values. So if you have multiple values with the same key, only the last one will be preserved.
<?php
$array1[0] = "zero";
$array1[1] = "one";
$array2[0] = 0;
$array1[1] = 1;
$array3 = $array1 + $array2;
// array3 will look like:
// array(0=>0, 1=>1)
// ie: beware of the latter keys overwriting the former keys
?>
In some situations, the union operator ( + ) might be more useful to you than array_merge. The array_merge function does not preserve numeric key values. If you need to preserve the numeric keys, then using + will do that.
ie:
<?php
$array1[0] = "zero";
$array1[1] = "one";
$array2[1] = "one";
$array2[2] = "two";
$array2[3] = "three";
$array3 = $array1 + $array2;
//This will result in::
$array3 = array(0=>"zero", 1=>"one", 2=>"two", 3=>"three");
?>
Note the implicit "array_unique" that gets applied as well. In some situations where your numeric keys matter, this behaviour could be useful, and better than array_merge.
--Julian
In both PHP 4 and 5, array_merge preserves references in array values. For example:
<?php
$foo = 12;
$array = array("foo" => &$foo);
$merged_array = array_merge($array, array("bar" => "baz"));
$merged_array["foo"] = 24;
assert($foo === 24); // works just fine
?>
I was looking to merge two associative arrays together, adding the values together if the keys were the same. If there were keys unique to either array, these would be added into the merged array with their existing values.
I couldnt find a function to do this, so made this:
<?php
function merge($array1, $array2)
{
if(sizeof($array1)>sizeof($array2))
{
echo $size = sizeof($array1);
}else{
$a = $array1;
$array1 = $array2;
$array2 = $a;
echo $size = sizeof($array1);
}
$keys2 = array_keys($array2);
for($i = 0;$i<$size;$i++)
{
$array1[$keys2[$i]] = $array1[$keys2[$i]] + $array2[$keys2[$i]];
}
$array1 = array_filter($array1);
return $array1;
}
?>
Enter in two arrays to the function, then the output will be both arrays merged together with values added if keys are the same.
There was a previous note that alluded to this, but at least in version 5.2.4 array_merge will return NULL if any of the arguments are NULL. This bit me with $_POST when none of the controls were successful.
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;
}
?>
I needed to merge two arrays while keeping the first arrays format and adding the values, if they exist, from the second array. I was working with several multidimensional arrays and was using one as the primary structure to insert data into a database. This is what I made. I hope it helps.
<?php
function array_overlay($a1,$a2)
{
foreach($a1 as $k => $v) {
if(!array_key_exists($k,$a2)) continue;
if(is_array($v) && is_array($a2[$k])){
$a1[$k] = array_overlay($v,$a2[$k]);
}else{
$a1[$k] = $a2[$k];
}
}
return $a1;
}
?>
I used it like this:
<?php
$table = tableDescription(); // created an array of the table structure
$table = array_overlay($table,$_POST); // writes the values submitted from the array into the table using array names in the form like <input type="text" name="column[Value]" value="" />
?>
Usage of operand '+' for merging arrays:
<?php
$a=array(
'a'=>'a1',
'b'=>'a2',
'a3',
'a4',
'a5');
$b=array('b1',
'b2',
'a'=>'b3',
'b4');
$a+=$b;
print_r($a);
?>
output:
Array
(
[a] => a1
[b] => a2
[0] => a3
[1] => a4
[2] => a5
[3] => b5
)
numeric keys of elements of array B what not presented in array A was added.
<?php
$a=array('a'=>'a1','b'=>'a2','a3','a4','a5');
$b=array(100=>'b1','b2','a'=>'b3','b4');
$a+=$b;
print_r($a);
?>
output:
[a] => a1
[b] => a2
[0] => a3
[1] => a4
[2] => a5
[100] => b1
[101] => b2
[102] => b4
autoindex for array B started from 100, these keys not present in array A, so this elements was added to array A
I needed a function similar to ian at fuzzygroove's array_interlace, but I need to pass more than two arrays.
Here's my version, You can pass any number of arrays and it will interlace and key them properly.
<?php
function array_interlace() {
$args = func_get_args();
$total = count($args);
if($total < 2) {
return FALSE;
}
$i = 0;
$j = 0;
$arr = array();
foreach($args as $arg) {
foreach($arg as $v) {
$arr[$j] = $v;
$j += $total;
}
$i++;
$j = $i;
}
ksort($arr);
return array_values($arr);
}
?>
Example usage:
<?php
$a = array('a', 'b', 'c', 'd');
$b = array('e', 'f', 'g');
$c = array('h', 'i', 'j');
$d = array('k', 'l', 'm', 'n', 'o');
print_r(array_interlace($a, $b, $c, $d));
?>
result:
Array
(
[0] => a
[1] => e
[2] => h
[3] => k
[4] => b
[5] => f
[6] => i
[7] => l
[8] => c
[9] => g
[10] => j
[11] => m
[12] => d
[13] => n
[14] => o
)
Let me know if you improve on it.
To avoid REINDEXING issues,
use + operator :
array_merge(
array("truc" => "salut"),
array("machin" => "coucou")
)
returns
array(2)
{
[0] => string() "salut"
[1] => string() "coucou"
}
whereas
array("truc" => "salut") + array("machin" => "coucou")
returns
array(2)
{
["truc"] => string() "salut"
["machin"] => string() "coucou"
}
I wrote this function to strip one or more arrays to a single one-dimensional array. Keys assigned to individual values are preserved; keys assigned to arrays are not.
<?php
function array_merge_deep($arr) { // an array-merging function to strip one or more arrays down to a single one dimension array
$arr = (array)$arr;
$argc = func_num_args();
if ($argc != 1) {
$argv = func_get_args();
for ($i = 1; $i < $argc; $i++) $arr = array_merge($arr, (array)$argv[$i]);
}
$temparr = array();
foreach($arr as $key => $value) {
if (is_array($value)) $temparr = array_merge($temparr, array_merge_deep($value));
else $temparr = array_merge($temparr, array($key => $value));
}
return $temparr;
}
?>
Example:
<?php
$groceries = array("fruits" => array("apples", "bananas", "cherries", "grapes"), "dry goods" => array("bread", "flour", "yeast"), "dairy" => "milk");
$groceries = array_merge_deep($groceries);
echo "<pre>"; print_r($groceries); echo "</pre>";
?>
Yields the following:
Array
(
[0] => apples
[1] => bananas
[2] => cherries
[3] => grapes
[4] => bread
[5] => flour
[6] => yeast
[dairy] => milk
)
probably simpler versions of this (below) but thought i'd post my solution up as it might be helpful :)
<?php
function array_merge_php4_comp() {
$n=func_num_args();
if( $n>0 ) {
$rval=func_get_arg(0);
if( !is_array($rval) ) {
$rval=(array)$rval;
}
if( $n>1 ) {
for( $i=1; $i<$n; $i++ ) {
$tval=func_get_arg($i);
if( !is_array($tval) ) {
$tval=(array)$tval;
}
$rval=array_merge($rval, $tval);
}
} else {
$rval=array_merge($rval);
}
} else {
$rval=array_merge();
}
return $rval;
}
?>
I needed a function to interlace two arrays ([a,b,c] + [d,e,f] = [a,d,b,e,c,f]) and came up with the following. This function works for arrays of varying lengths and keeps the keys sequenced properly.
<?php
$a = array ('a', 'b', 'c');
$b = array ('d', 'e', 'f', 'g', 'h', 'i');
$c = array_interlace ($a, $b);
/*
Array
(
[0] => a
[1] => d
[2] => b
[3] => e
[4] => c
[5] => f
[6] => g
[7] => h
[8] => i
)
*/
function array_interlace ($a, $b)
{
$c = array();
$shorty = (count($a) > count($b)) ? $b : $a;
$biggy = (count($a) > count($b)) ? $a : $b;
$slen = count($shorty);
$blen = count($biggy);
for ($i = 0; $i < $slen; ++$i){
$c[$i * 2] = $a[$i];
$c[$i * 2 + 1] = $b[$i];
}
for ($i = $slen; $i < $blen; ++$i)
{
$c[] = $biggy[$i];
}
return $c;
}
?>
The documentation is a touch misleading when it says: "If only one array is given and the array is numerically indexed, the keys get reindexed in a continuous way." Even with two arrays, the resulting array is re-indexed:
[bishop@predator staging]$ cat array_merge.php
<?php
$a = array (23 => 'Hello', '42' => 'World');
$a = array_merge(array (0 => 'I say, '), $a);
var_dump($a);
?>
[bishop@predator staging]$ php-5.2.5 array_merge.php
array(3) {
[0]=>
string(7) "I say, "
[1]=>
string(5) "Hello"
[2]=>
string(5) "World"
}
[bishop@predator staging]$ php-4.4.7 array_merge.php
array(3) {
[0]=>
string(7) "I say, "
[1]=>
string(5) "Hello"
[2]=>
string(5) "World"
}
PHP is wonderfully decides if an array key could be a number, it is a number! And thus wipes out the key when you array merge. Just a warning.
$array1['4000'] = 'Grade 1 Widgets';
$array1['4000a'] = 'Grade A Widgets';
$array2['5830'] = 'Grade 1 Thing-a-jigs';
$array2['HW39393'] = 'Some other widget';
var_dump($array1);
var_dump($array2);
//results in...
array(2) {
[4000]=>
string(15) "Grade 1 Widgets"
["4000a"]=>
string(15) "Grade A Widgets"
}
array(2) {
[5830]=>
string(20) "Grade 1 Thing-a-jigs"
["HW39393"]=>
string(17) "Some other widget"
}
var_dump(array_merge($array1,$array2));
//results in...
array(4) {
[0]=>
string(15) "Grade 1 Widgets"
["4000a"]=>
string(15) "Grade A Widgets"
[1]=>
string(20) "Grade 1 Thing-a-jigs"
["HW39393"]=>
string(17) "Some other widget"
}
As has already been noted before, reindexing arrays is most cleanly performed by the array_values() function.
The notes in Example 257 above are incorrect. It states:
If you want to completely preserve the arrays and just want to append them to each other, use the + operator
Check the array type page at http://www.php.net/manual/en/language.types.array.php which states differently. Duplicate keys are ignored with the + operator.
Similar to Jo I had a problem merging arrays (thanks for that Jo you kicked me out of my debugging slumber) - array_merge does NOT act like array_push, as I had anticipated
<?php
$array = array('1', 'hello');
array_push($array, 'world');
var_dump($array);
// gives '1', 'hello', 'world'
$array = array('1', 'hello');
array_merge($array, array('world'));
// gives '1', 'hello'
$array = array('1', 'hello');
$array = array_merge($array, array('world'));
// gives '1', 'hello', 'world'
?>
hope this helps someone
I found the "simple" method of adding arrays behaves differently as described in the documentation in PHP v5.2.0-10.
$array1 + $array2 will only combine entries for keys that don't already exist.
Take the following example:
$ar1 = array('a', 'b');
$ar2 = array('c', 'd');
$ar3 = ($ar1 + $ar2);
print_r($ar3);
Result:
Array
(
[0] => a
[1] => b
)
Where as:
$ar1 = array('a', 'b');
$ar2 = array('c', 'd');
$ar3 = array_merge($ar1, $ar2);
print_r($ar3);
Result:
Array
(
[0] => a
[1] => b
[2] => c
[3] => d
)
Hope this helps someone.
For asteddy at tin dot it and others who are trying to merge arrays and keep the keys, don't forget the simple + operator.
Using the array_merge_keys() function (with a small mod to deal with multiple arguments), provides no difference in output as compared to +.
<?php
$a = array(-1 => 'minus 1');
$b = array(0 => 'nought');
$c = array(0 => 'nought');
var_export(array_merge_keys($a,$b));
var_export($a + $b);
var_export(array_merge_keys($a,$b,$c));
var_export($a + $b + $c);
?>
results in ...
array ( -1 => 'minus 1', 0 => 'nought',)
array ( -1 => 'minus 1', 0 => 'nought',)
array ( -1 => 'minus 1', 0 => 'nought',)
array ( -1 => 'minus 1', 0 => 'nought',)
I have been searching for an in-place merge function, but couldn't find one. This function merges two arrays, but leaves the order untouched.
Here it is for all others that want it:
function inplacemerge($a, $b) {
$result = array();
$i = $j = 0;
if (count($a)==0) { return $b; }
if (count($b)==0) { return $a; }
while($i < count($a) && $j < count($b)){
if ($a[$i] <= $b[$j]) {
$result[] = $a[$i];
if ($a[$i]==$b[$j]) { $j++; }
$i++;
} else {
$result[] = $b[$j];
$j++;
}
}
while ($i<count($a)){
$result[] = $a[$i];
$i++;
}
while ($j<count($b)){
$result[] = $b[$j];
$j++;
}
return $result;
}
An earlier comment mentioned that array_splice is faster than array_merge for inserting values. This may be the case, but if your goal is instead to reindex a numeric array, array_values() is the function of choice. Performing the following functions in a 100,000-iteration loop gave me the following times: ($b is a 3-element array)
array_splice($b, count($b)) => 0.410652
$b = array_splice($b, 0) => 0.272513
array_splice($b, 3) => 0.26529
$b = array_merge($b) => 0.233582
$b = array_values($b) => 0.151298
I recently upgrade from PHP4 to PHP5. While using PHP4, there was a certain set of information that we filtered into the $_SESSION global at login. i.e.
$_SESSION = array_merge( $_SESSION, $additional_vals );
This method worked great in PHP4, but not in PHP5. I can only assume the session immediately invalidated itself.
In fact, my tests to find this bug, led me to find out that all session changes - even those before this action - were lost.
DO NOT USE array_merge() FROM $_SESSION AND TO $_SESSION
Sorry, I forgot to translate the the name of the function when it calls itself. Here is the correct code:
<?
/*
IT KEEPS ALL VALUES OF $arr1 AND
ADDS ALL KEYS AND VALUES OF $arr2 THAT ARE NOT IN $arr1
*/
function array_merge_keys($arr1, $arr2) {
foreach($arr2 as $k=>$v) {
if (!array_key_exists($k, $arr1)) { //K DOESN'T EXISTS //
$arr1[$k]=$v;
}
else { // K EXISTS //
if (is_array($v)) { // K IS AN ARRAY //
$arr1[$k]=array_merge_keys($arr1[$k], $arr2[$k]);
}
}
}
return $arr1;
}
?>
/*
IT KEEPS ALL VALUES OF $arr1 AND
ADDS ALL KEYS AND VALUES OF $arr2 THAT ARE NOT IN $arr1
*/
function array_merge_keys($arr1, $arr2) {
foreach($arr2 as $k=>$v) {
if (!array_key_exists($k, $arr1)) { //K DOESN'T EXISTS //
$arr1[$k]=$v;
}
else { // K EXISTS //
if (is_array($v)) { // K IS AN ARRAY //
$arr1[$k]=array_unisci_chiavi($arr1[$k], $arr2[$k]);
}
}
}
return $arr1;
}
To: zspencer at zacharyspencer dot com
You forget about code of this function.
<?
function safe_array_merge($a, $b) {
foreach ($b as $v) {
$a[] = $v;
}
return $a;
}
?>
I noticed the lack of a function that will safely merge two arrays without losing data due to duplicate keys but different values.
So I wrote a quicky that would offset duplicate keys and thus preserve their data. of course, this does somewhat mess up association...
<?php
$array1=array('cats'=>'Murder the beasties!', 'ninjas'=>'Use Ninjas to murder cats!');
$array2=array('cats'=>'Cats are fluffy! Hooray for Cats!', 'ninjas'=>'Ninas are mean cat brutalizers!!!');
$array3=safe_array_merge($array1, $array2);
print_r($array3)
/*
Array {
cats => Murder the beasties!,
ninjas => Use ninjas to murder cats!,
?>
cats_0 => Cats are fluffy! Hooray for Cats!,
ninjas_0 => Ninjas are mean cat brutalizers!!!
}
function safe_array_merge ()
{
$args = func_get_args();
$result=array();
foreach($args as &$array)
{
foreach($array as $key=>&$value)
{
if(isset($result[$key]))
{
$continue=TRUE;
$fake_key=0;
while($continue==TRUE)
{
if(!isset($result[$key.'_'.$fake_key]))
{
$result[$key.'_'.$fake_key]=$value;
$continue=FALSE;
}
$fake_key++;
}
}
else
{
$result[$key]=$value;
}
}
}
return $result;
}
?>
A more efficient array_merge that preserves keys, truly accepts an arbitrary number of arguments, and saves space on the stack (non recursive):
<?php
function array_merge_keys(){
$args = func_get_args();
$result = array();
foreach($args as &$array){
foreach($array as $key=>&$value){
$result[$key] = $value;
}
}
return $result;
}
?>
Needed an quick array_merge clone that preserves the keys:
<?
// function array_join
// merges 2 arrays preserving the keys,
// even if they are numeric (unlike array_merge)
// if 2 keys are identical, the last one overwites
// the existing one, just like array_merge
// merges up to 10 arrays, minimum 2.
function array_join($a1, $a2, $a3=null, $a4=null, $a5=null, $a6=null, $a7=null, $a8=null, $a9=null, $a10=null) {
$a=array();
foreach($a1 as $key=>$value) $a[$key]=$value;
foreach($a2 as $key=>$value) $a[$key]=$value;
if (is_array($a3)) $a=array_join($a,$a3,$a4,$a5,$a6,$a7,$a8,$a9,$a10);
return $a;
}
?>
You can use array_slice() in combination with array_merge() to insert values into an array like this:
<?php
$test=range(0, 10);
$index=2;
$data="---here---";
$result=array_merge(array_slice($test, 0, $index), array($data), array_slice($test, $index));
var_dump($result);
?>
array_merge() overwrites ALL numerical indexes. No matter if you have non-numerical indexes or more than just one array.
It reindexes them all. Period.
(Only tried in 4.3.10)
Whoops!!! The 2nd posting before this one about the array_merge_alternative1() I made. Replace part of that code with this one below...
--snip--
/* 07/20/2006 - Added the if statement to avoid the warning message spitted out by array_unique() function, "Warning: Wrong datatype in array_unique() call"... */
if ($xyz != 0) {
$new_array2 = array_unique($new_array1); /* Work a lot like DISTINCT() in SQL... */
} else {
$new_array2 = array();
}
--snip--
Because the unique_array() unexpectly give a warning message when it's empty... Sorry about that...
I don't think that the comment on + operator for array in array_merge page, was understandable, this is just a little test to know exactly what's happend.
<?php
//test code for (array)+(array) operator
$a1 = array( '10', '11' , '12' , 'a' => '1a', 'b' => '1b');
$a2 = array( '20', '21' , '22' , 'a' => '2a', 'c' => '2c');
$a = $a1 + $a2;
print_r( $a );
//result: Array ( [0] => 10
// [1] => 11
// [2] => 12
// [a] => 1a
// [b] => 1b
// [c] => 2c )
$a = $a2 + $a1;
print_r( $a );
//result: Array ( [0] => 20
// [1] => 21
// [2] => 22
// [a] => 2a
// [c] => 2c
// [b] => 1b )
?>
I found that array_merge() didn't work in my case as it should have because I still get duplicate values. Again, I noticed that the values can be overwritten if the array's key are the same as stated in the manual.
So, I came up with the alternative and it work like a charm.
--snip--
function array_merge_alternative1() {
//echo func_num_args(); /* Get the total # of arguements (parameter) that was passed to this function... */
//print_r(func_get_arg()); /* Get the value that was passed in via arguement/parameter #... in int, double, etc... (I think)... */
//print_r(func_get_args()); /* Get the value that was passed in via arguement/parameter #... in arrays (I think)... */
$loop_count1 = func_num_args();
$junk_array1 = func_get_args();
$xyz = 0;
for($x=0;$x<$loop_count1;$x++) {
$array_count1 = count($junk_array1[$x]);
if ($array_count1 != 0) {
for($y=0;$y<$array_count1;$y++) {
$new_array1[$xyz] = $junk_array1[$x][$y];
$xyz++;
}
}
}
$new_array2 = array_unique($new_array1); /* Work a lot like DISTINCT() in SQL... */
return $new_array2;
}
--snip--
Cheer...
Do not use this to set the $_SESSION variable.
$_SESSION = array_merge( $_SESSION, $another_array );
will break your $_SESSION until the end of the execution of that page.
I needed a function to alternatly merge 2 arrays. i.e. $a1 = (a, c, e) and $a2 = (b, d, f), and now $merged = (a, b, c, d, e, f).
Hope it helps...
<?
function alternatly_merge_arrays($array1, $array2){
$combined_array = array();
$count = 0;
for($i = 0; $i < count($array1) + count($array2); $i = $i + 2){
$combined_array[$i] = $array1[$count];
$count++;
}//end for
$count = 0;
for($i = 1; $i < count($array1) + count($array2); $i = $i + 2){
$combined_array[$i] = $array2[$count];
$count++;
}//end for
return $combined_array;
}//end alternate_merge_arrays
?>
I forgot to comment. This function join two arrays adding first the elements equals in the two arrays, then the values of the first array and at the end the values of the second array.
<?php
function array_merge_sim ($arr1, $arr2) {
$i=0;
$adid_arrayS = array();
foreach ($arr1 as $x) {
$j=0;
$nr1=count($arr1);
$nr2=count($arr2);
foreach ($arr2 as $z) {
if($x==$z){
array_push($adid_arrayS,$z);
if($nr2==($j+1)) array_pop($arr2);
else array_splice($arr2, $j,-($nr2-$j-1));
if($nr1==($i+1)) array_pop($arr1);
else array_splice($arr1, $i,-($nr1-$i-1));
$i--;
}else $j++;
}
$i++;
}
return array_merge($adid_arrayS,$arr1,$arr2);
}
?>
Since my comment from 12-Jul-2004 02:42 with <? function array_merge_php4() ?> was deleted and the complex one added by Barpfotenbaer (09-Jul-2005 06:33), - here is the simpler and faster solution:
<?
/* works only with PHP 5 */
function array_merge_php4($array1, $array2) {
foreach ($args = func_get_args() as &$arg) {
$arg = (array)$arg;
}
return call_user_func_array('array_merge',$args);
}
?>
The same result as produced by snookiex_at_gmail_dot_com's function
can be achieved with the 'one-liner'
<?php
$array1=array(1,2,3);
$array2=array('a','b','c');
$matrix = array_map(null, $array1, $array2);
?>
(see documentation of array_map).
The difference here is, that the shorter array gets filled with empty values.
if you have, for instance:
$array1=array(1,2,3);
$array2=array('a','b','c');
but you want a 2x3 matrix like this:
1 a
2 b
3 c
use:
function arrays2matrix($array1,$array2){
if(sizeof($array1) != sizeof($array2))
return;
for($i=0;$i<sizeof($array1);$i++)
$res[]=array_merge($array1[$i],$array2[$i]);
return $res;
}
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 '_'.
If you need to merge two arrays without having multiple entries, try this:
<?php
function array_fusion($ArrayOne, $ArrayTwo)
{
return array_unique(array_merge($ArrayOne, $ArrayTwo));
}
?>
<?
/*
Warning: if one variable is not an array, the merged array will
have one more line!
*/
$a = "";
$b[0] = "Brazil";
$b[1] = "China";
$c = array_merge($a, $b);
print_r($c);
?>
This returns:
Array
(
[0] =>
[1] => Brazil
[2] => China
)
__________________________________
Rafael Pereira dos Santos
I wanted to share this, it's an small simple script to add things into an array using an loop/routine.
I use an more developed version to sort msessage's on a forum.
<?php
// Make vars ready
$a=0;
$alltopic = array();
// Start an routine
// 4 is just an example you can use an varible containing
// the number of rows in an databse for example
while ($a < 4)
{
$topic = array($a => "$a");
$alltopic = array_merge ($alltopic, $topic);
$a++;
};
// Put it on screen:
print_r($alltopic);
?>
outputs this :
Array ( [0] => 0 [1] => 1 [2] => 2 [3] => 3 )
<?php
/*
* array_deep_merge
*
* array array_deep_merge ( array array1 [, array array2 [, array ...]] )
*
* Like array_merge
*
* array_deep_merge() 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 input arrays have the same string keys, then the later value for
* that key will overwrite the previous one. If, however, the arrays contain
* numeric keys, the later value will not overwrite the original value, but
* will be appended.
* If only one array is given and the array is numerically indexed, the keys
* get reindexed in a continuous way.
*
* Different from array_merge
* If string keys have arrays for values, these arrays will merge recursively.
*/
function array_deep_merge()
{
switch( func_num_args() )
{
case 0 : return false; break;
case 1 : return func_get_arg(0); break;
case 2 :
$args = func_get_args();
$args[2] = array();
if( is_array($args[0]) and is_array($args[1]) )
{
foreach( array_unique(array_merge(array_keys($args[0]),array_keys($args[1]))) as $key )
if( is_string($key) and is_array($args[0][$key]) and is_array($args[1][$key]) )
$args[2][$key] = array_deep_merge( $args[0][$key], $args[1][$key] );
elseif( is_string($key) and isset($args[0][$key]) and isset($args[1][$key]) )
$args[2][$key] = $args[1][$key];
elseif( is_integer($key) and isset($args[0][$key]) and isset($args[1][$key]) ) {
$args[2][] = $args[0][$key]; $args[2][] = $args[1][$key]; }
elseif( is_integer($key) and isset($args[0][$key]) )
$args[2][] = $args[0][$key];
elseif( is_integer($key) and isset($args[1][$key]) )
$args[2][] = $args[1][$key];
elseif( ! isset($args[1][$key]) )
$args[2][$key] = $args[0][$key];
elseif( ! isset($args[0][$key]) )
$args[2][$key] = $args[1][$key];
return $args[2];
}
else return $args[1]; break;
default :
$args = func_get_args();
$args[1] = array_deep_merge( $args[0], $args[1] );
array_shift( $args );
return call_user_func_array( 'array_deep_merge', $args );
break;
}
}
/*
* test
*/
$a = array(
0,
array( 0 ),
'integer' => 123,
'integer456_merge_with_integer444' => 456,
'integer789_merge_with_array777' => 789,
'array' => array( "string1", "string2" ),
'array45_merge_with_array6789' => array( "string4", "string5" ),
'arraykeyabc_merge_with_arraykeycd' => array( 'a' => "a", 'b' => "b", 'c' => "c" ),
'array0_merge_with_integer3' => array( 0 ),
'multiple_merge' => array( 1 ),
);
$b = array(
'integer456_merge_with_integer444' => 444,
'integer789_merge_with_array777' => array( 7,7,7 ),
'array45_merge_with_array6789' => array( "string6", "string7", "string8", "string9" ),
'arraykeyabc_merge_with_arraykeycd' => array( 'c' => "ccc", 'd' => "ddd" ),
'array0_merge_with_integer3' => 3,
'multiple_merge' => array( 2 ),
);
$c = array(
'multiple_merge' => array( 3 ),
);
echo "<pre>".htmlentities(print_r( array_deep_merge( $a, $b, $c ), true))."</pre>";
?>
Old behavior of array_merge can be restored by simple variable type casting like this
array_merge((array)$foo,(array)$bar);
works good in php 5.1.0 Beta 1, not tested in other versions
seems that empty or not set variables are casted to empty arrays
Topic: The modified behavior of array_merge() in PHP5: "array_merge() now only accepts parameters of type array"
A very simple way to simulate the old behavior of PHP4 to merge arrays with non-arrays:
<?php
function array_merge_php4 ()
{
$array["merged"]=array ();
for($i=0;$i<func_num_args ();$i++)
{
$array["tmp"]=(
(is_array (func_get_arg ($i)))?
(func_get_arg ($i)):
(array (func_get_arg ($i)))
);
$array["merged"]=array_merge ($array["merged"],$array["tmp"]);
}
return($array["merged"]);
}
?>
About the behavior of array_merge() that was modified in PHP5. The above warning says "array_merge() now only accepts parameters of type array". But it doesn't say what happens when array_merge() is given a non array parameter like in:
<?php
error_reporting(E_STRICT);
$a = array("red","green");
$rgb = array_merge($a,"blue");
?>
In fact, the PHP5 version of array_merge() will perfectly accept the "blue" parameter and will return... the NULL value! Be aware that you will get no warning even with the error reporting level set to E_STRICT (which is supposed to catch "forward compatibility" troubles).
As "rcarvalhoREMOVECAPS at clix dot pt" suggested above,
$a = array_merge($a, null);
does renumber the $a array. However, this only works with PHP4, not with PHP5. PHP5 users might try the following:
$b = implode(" ", $a);
$a = explode(" ", $b);
Hope this helps someone too ;-)
To insert an array inside another at a given position
<?php
//Insert at $pos array $items inside array $source
function array_insert($source, $pos, $items)
{
$sub1 = array_slice($source, 0, $pos);
$sub2 = array_slice($source, $pos);
return array_merge($sub1,$items,$sub2);
}
// 2nd alternative (Note: it changes $source)
function array_insert($source, $pos, $items)
{
$rest = array_splice($source, $pos);
return $source = array_merge($source,$items,$rest);
}
?>
Here are a few functions to make a diff between two arrays (or 2 strings, see the example) and merge it back after
<?
function array_diff_both($new,$old)
{
$del=array_diff_assoc($old,$new);
$add=array_diff_assoc($new,$old);
return $diff=array("del"=>$del, "add"=>$add);
}
function array_diff_all($arr_new,$arr_old)
{
$arr_equ=array_intersect_assoc($arr_new,$arr_old);
$arr_del=array_diff_assoc($arr_old,$arr_new);
$arr_add=array_diff_assoc($arr_new,$arr_old);
return $diff=array("equ"=>$arr_equ, "del"=>$arr_del, "add"=>$arr_add);
}
function array_merge_diff($arr,$diff) {
$arr=array_merge_replace($arr,$diff["del"]);
$arr=array_merge_replace($arr,$diff["add"]);
return $arr;
}
function array_merge_diff_reverse($arr,$diff) {
$arr=array_merge_replace($arr,$diff["add"]);
$arr=array_merge_replace($arr,$diff["del"]);
return $arr;
}
?>
Here is an example:
<?
//calculationg
$new="hello!"; // new string
$old="hrllo!"; // old string with a typo fault
// cast string in array the goodway
// make a cast (array) to a string does not work:
// the entire string is thus considered as one element
// of the array !!
$a_new=str_split($new);
$a_old=str_split($old); // cast string in array
$diff=array_diff_both($a_new,$a_old); // the entire diff
$test=array_merge_diff($a_old,$diff);
$test_reverse=array_merge_diff_reverse($a_new,$diff);
//for output the sample
print "new= $new<br/>old= $old<br/>\$a_new= ";
print_r($a_new);
print "<br/>\$a_old= ";
print_r($a_old);
print "<br/>\$diff=\$a_new-\$a_old";
print "<br/>\$diff= ";
print_r($diff);
print "<br/>\$test=\$a_old+\$diff";
print "<br/>\$test= ";
print_r($test);
print("<br/>".implode($test));
print "<br/>\$test_reverse=\$a_new+\$diff";
print "<br/>\$test_reverse= ";
print_r($test_reverse);
print("<br/>".implode($test_reverse));
?>
En sortie de l'exemple, résultats:
new= hello!
old= hrllo!
$a_new= Array ( [0] => h [1] => e [2] => l [3] => l [4] => o [5] => ! )
$a_old= Array ( [0] => h [1] => r [2] => l [3] => l [4] => o [5] => ! )
$diff=$a_new-$a_old
$diff= Array ( [del] => Array ( [1] => r ) [add] => Array ( [1] => e ) )
$test=$a_old+$diff
$test= Array ( [0] => h [1] => e [2] => l [3] => l [4] => o [5] => ! )
hello!
$test_reverse=$a_new+$diff
$test_reverse= Array ( [0] => h [1] => r [2] => l [3] => l [4] => o [5] => ! )
hrllo!
autre exemple, autre résultat:
new= see you!
old= sea ya!
$a_new= Array ( [0] => s [1] => e [2] => e [3] => [4] => y [5] => o [6] => u [7] => ! )
$a_old= Array ( [0] => s [1] => e [2] => a [3] => [4] => y [5] => a [6] => ! )
$diff=$a_new-$a_old
$diff= Array ( [del] => Array ( [2] => a [5] => a [6] => ! ) [add] => Array ( [2] => e [5] => o [6] => u [7] => ! ) )
$test=$a_old+$diff
$test= Array ( [0] => s [1] => e [2] => e [3] => [4] => y [5] => o [6] => u [7] => ! )
see you!
$test_reverse=$a_new+$diff
$test_reverse= Array ( [0] => s [1] => e [2] => a [3] => [4] => y [5] => a [6] => ! [7] => ! )
sea ya!!
The following function merges two arrays taking as parameter an user-defined comparison function:
<?
function array_merge_custom(&$array1,&$array2,$comparison_func)
{
$ret_arr = array();
foreach ($array1 as $key1 => $element1)
{
$has_equal_in_2 = false;
foreach($array2 as $key2 => $element2)
{
if (call_user_func($comparison_func,$element1,$element2))
{
$has_equal_in_2 = true;
}
}
if (!$has_equal_in_2)
{
$ret_arr[] = $array1[$key1];
}
}
$ret_arr = array_merge($ret_arr,$array2);
return $ret_arr;
}
?>
For instance:
<?
$a = array(
array('index' => 'ball', 'blabla' => 0),
array('index' => 'coconut'),
array('index' => 'cow'),
array('index' => 'son'),
);
$b = array(
array('index' => 'phone'),
array('index' => 'wallet'),
array('index' => 'ball'),
array('index' => 'son', 'kabum'=> 1)
);
$c = array_merge_custom($a,$b,create_function('$a,$b','return $a[\'index\'] == $b[\'index\'];'));
print_r($c)
?>
Will produce:
Array
(
[0] => Array
(
[index] => coconut
)
[1] => Array
(
[index] => cow
)
[2] => Array
(
[index] => phone
)
[3] => Array
(
[index] => wallet
)
[4] => Array
(
[index] => ball
)
[5] => Array
(
[index] => son
[kabum] => 1
)
)
Note that casting the array doesn't always work. For classes, it looks like the class information is lost. The print_r's below have different behavior. I would suggest creating an array, rather than casting in most situations. (ie. do it like $array2).
class myclass {
private $name;
private $value;
function myclass($n, $v) {
$this->name=$n;
$this->value=$v;
}
}
$itema = new myclass("hair", "orange");
$itemb = new myclass("eyes", "green");
$itemc = new myclass("elbow", "dirty");
$items = array($itemb, $itemc);
print "<BR>";
$array1 = array_merge((array)$itema, $items);
$array2 = array_merge(array($itema), $items);
print "<BR>";
print "<BR>";
print_r($array1);
print "<BR>";
print "<BR>";
print_r($array2);
Array ( [] => orange [0] => myclass Object ( [name:private] => eyes [value:private] => green ) [1] => myclass Object ( [name:private] => elbow [value:private] => dirty ) )
Array ( [0] => myclass Object ( [name:private] => hair [value:private] => orange ) [1] => myclass Object ( [name:private] => eyes [value:private] => green ) [2] => myclass Object ( [name:private] => elbow [value:private] => dirty ) )
This is a simple function to merge an array multidimensional:
<?php
function multimerge ($array1, $array2) {
if (is_array($array2) && count($array2)) {
foreach ($array2 as $k => $v) {
if (is_array($v) && count($v)) {
$array1[$k] = multimerge($array1[$k], $v);
} else {
$array1[$k] = $v;
}
}
} else {
$array1 = $array2;
}
return $array1;
}
$array1 = array(
1 => "value1",
2 => "value2",
3 => array(
"valor2" => "test3",
),
"more" => array(
"alot" => "A lot of values",
":)" => "Cont"
)
);
$array2 = array(
2 => "other value",
3 => array(
"valor3" => "more values",
),
"more" => array(
"alot" => "Change the value"
),
"final" => "Final Value"
);
print_r(multimerge($array1, $array2));
?>
The output is:
Array
(
[1] => value1
[2] => other value
[3] => Array
(
[valor2] => test3
[valor3] => more values
)
[more] => Array
(
[alot] => Change the value
[:)] => Cont
)
[final] => Final Value
)
...
function preserved_merge_array( $newArray, $otherArray ) {
foreach( $otherArray as $key => $value)
{
if ( !is_array($newArray[$key]) ) $newArray[$key] = array();
if ( is_array($value) ) $newArray[$key] = preserved_merge_array( $newArray[$key], $value );
else $newArray[$key] = $value;
}
return $newArray;
}
...
Note that if you put a number as a key in an array, it is eventually converted to an int even if you cast it to a string or put it in quotes.
That is:
$arr["0"] = "Test";
var_dump( key($arr) );
will output int(0).
This is important to note when merging because array_merge will append values with a clashing int-based index instead of replacing them. This kept me tied up for hours.
Someone posted a function with the note:
"if u need to overlay a array that holds defaultvalues with another that keeps the relevant data"
<?
//about twice as fast but the result is the same.
//note: the sorting will be messed up!
function array_overlay($skel, $arr) {
return $arr+$skel;
}
//example:
$a = array("zero","one","two");
$a = array_overlay($a,array(1=>"alpha",2=>NULL));
var_dump($a);
/* NULL is ignored so the output is:
array(3) {
[1]=>
string(5) "alpha"
[0]=>
string(4) "zero"
[2]=>
string(3) "two"
}
*/
?>
if you generate form select from an array, you probably want to keep your array keys and order intact,
if so you can use ArrayMergeKeepKeys(), works just like array_merge :
array ArrayMergeKeepKeys ( array array1 [, array array2 [, array ...]])
but keeps the keys even if of numeric kind.
enjoy
<?
$Default[0]='Select Something please';
$Data[147]='potato';
$Data[258]='banana';
$Data[54]='tomato';
$A=array_merge($Default,$Data);
$B=ArrayMergeKeepKeys($Default,$Data);
echo '<pre>';
print_r($A);
print_r($B);
echo '</pre>';
Function ArrayMergeKeepKeys() {
$arg_list = func_get_args();
foreach((array)$arg_list as $arg){
foreach((array)$arg as $K => $V){
$Zoo[$K]=$V;
}
}
return $Zoo;
}
//will output :
Array
(
[0] => Select Something please
[1] => potato
[2] => banana
[3] => tomato
)
Array
(
[0] => Select Something please
[147] => potato
[258] => banana
[54] => tomato
)
?>
If you have a two dimensional array, or otherwise an array of arrays, the following may be handy: <?call_user_func_array('array_merge', $arr);?>
For instance:
<?php
list($GIF, $JPG, $PNG, $WBMP) = array(1,2,3,15);
$ext_map = array(
$GIF => array('gif'),
$JPG => array('jpg', 'jpeg'),
$PNG => array('png'),
$WBMP => array('wbmp'),
);
$ext_list = call_user_func_array('array_merge', $ext_map);
print_r($ext_list);
?>
Output:
Array
(
[0] => gif
[1] => jpg
[2] => jpeg
[3] => png
[4] => wbmp
)
This is explained above but may not be exactly clear. If you have two arrays such as:
$Array1 {
[0] => "dog"
}
$Array2 {
[0] => "cat",
[1] => "bird",
[2] => null,
...
[n-1] => null,
[n] => "gator"
}
and you don't want to lose the null values in the final array then do NOT use array_merge. Instead simply use the + operator.
array_merge($Array1,$Array2) gives:
[0] => "dog",
[1] => "cat",
[2] => "bird",
[3] => "gator"
$Array1 + $Array2 gives:
[0] => "dog",
[1] => "cat",
[2] => "bird",
[3] => null,
...
[n-1] => null,
[3] => "gator"
function array_overlay($skel, $arr)
{
foreach ($skel as $key => $val) {
if (!isset($arr[$key])) {
$arr[$key] = $val;
} else {
if (is_array($val)) {
$arr[$key] = array_overlay($val, $arr[$key]);
}
}
}
return $arr;
}
// if u need to overlay a array that holds defaultvalues with another that keeps the relevant data
I needed to add some values (array) to an array on each loop.
I used merge because it seemed to be the right way. But array_splice() ist much faster:
<?
$merge = array();
$splice = array();
function makearray() { //to do on each loop
//return two values:
return array('foo','bar');
}
for($i=0;$i<1000;$i++) $merge = array_merge($merge,makearray());
echo count($merge); //returns 2000 after about 2 sec (PHP5)
for($i=0;$i<1000;$i++) array_splice($splice, 0,0,makearray());
echo count($splice); //returns 2000 after about 0.8 sec (PHP5)
?>
Note: The sorting will be diffrent but you can use
array_splice($splice, count($splice),0,makearray());
which is still faster (1.4 sec)
It would seem that array_merge doesn't do anything when one array is empty (unset):
<?php //$a is unset
$b = array("1" => "x");
$a = array_merge($a, $b});
//$a is still unset.
?>
to fix this omit $a if it is unset:-
<?php
if(!IsSet($a)) {
$a = array_merge($b);
} else {
$a = array_merge($a, $b);
}
?>
I don't know if there's a better way.
[Editor's Note: If the keys are truly numeric, array_merge will do the trick. But if they are strings, as the example below, you can use array_merge(array_values($array1), array_values($array2)); since it's more beautiful. -- victor@php.net]
i recognized there is no function with that you can add two number-Arrays:
Example:
<?php
// you have two arrays:
array1 = array (['0'] =>"blahblupp",
['1'] => "bluppblah" );
array2 = array (['0'] =>"tirili",
['1'] => "tralala" );
// and want following as a result:
result = array (['0'] =>"blahblupp",
['1'] => "bluppblah",
['2'] =>"tirili",
['3'] => "tralala" );
// following function does the addition:
function array_add($array1, $array2)
{
$result = $array1;
$h = sizeof($array1);
for ($i = 0; $i < sizeof($array2); $i++)
{
$result[$h] = $array2[$i];
$h++;
}
return $result;
}
?>
just added because I didn't find a ready-to-use script here.
hey there, here is a nice little way of merging two arrays that may or may not have numerical or string indices and preserving the order and indexing of both arrays whilst making the most use of in built php functions (i actually originally wanted to use array_combine for obvious reasons but seeing as that is not available in PHP4, the little foreach loop at the end is necessary):
<?php
function merge($arr1,$arr2)
{
if(!is_array($arr1))
$arr1 = array();
if(!is_array($arr2))
$arr2 = array();
$keys1 = array_keys($arr1);
$keys2 = array_keys($arr2);
$keys = array_merge($keys1,$keys2);
$vals1 = array_values($arr1);
$vals2 = array_values($arr2);
$vals = array_merge($vals1,$vals2);
$ret = array();
foreach($keys as $key)
{
list($unused,$val) = each($vals);
$ret[$key] = $val;
}
return $ret;
}
?>
to merge arrays and preserve the key i found the following working with php 4.3.1:
<?php
$array1 = array(1 => "Test1", 2 => "Test2");
$array2 = array(3 => "Test3", 4 => "Test4");
$array1 += $array2;
?>
dont know if this is working with other php versions but it is a simple and fast way to solve that problem.
Here is something for splitting a sorted array into group array ( sorting the group arrays ) and then merging them together
Note: merge is like concat in Flash ActionScript :)
In this example i have 3 different groups in one array
* First the mainArray is sorted for the group entrys
* building from part arrays
* building copy arrays from part arrays ( for making the array beginning with [0] !! )
* merging all part arrays together
0. sorting the main array
1. apportionment from the main array
when the group is changing , a new array is build (part
array ) ! attention at the second part array !! Array values
are are not beginning with [0]!!
2. making the copy from the part array .
array values are pushed in the new array
3. echo for every array entry
4. overwriting the old array with the new copy
5. part arrays are sorted by function (arrays had been naughted by function array_copy ( Array is beginning with [0] ! )
6. merging the part arrays
Here is the same that i posted before but a bit better solved
array_push (${"activ_array".$j}, $activ_array[$i]); makes the part arrays starting with [0] :)
<?php
$activ_array=array(
array("apfel_01","germany","xx","red"),
array("apfel_07","germany","xx","green"),
array("apfel_08","germany","xx","green"),
array("apfel_02","germany","xx","red"),
array("apfel_03","germany","xx","red"),
array("apfel_04","germany","xx","red"),
array("apfel_09","germany","xx","red_+_green"),
array("apfel_10","germany","xx","red_+_green"),
array("apfel_05","germany","xx","green"),
array("apfel_06","germany","xx","green")
);
function array_sort($array, $key , $target){
for ($i = 0; $i < sizeof($array); $i++){
$array2 = $array;
if($target == 'name'){
if(! empty($array2[$i]["nameletter"])){
//echo $myarray[$i]["nameletter"] ;
$array2[$i]["name"] = substr($array2[$i][$key],($array2[$i]["nameletter"]-1));
}else{
$array2[$i]["name"] = substr($array2[$i][$key],0);
}
$sort_values[$i] = $array2[$i]["name"];
}else{
$sort_values[$i] = $array2[$i][$key];
}
}
asort ($sort_values);
reset ($sort_values);
while (list ($arr_keys, $arr_values) = each ($sort_values)) {
$sorted_arr[] = $array[$arr_keys];
}
return $sorted_arr;
}
$activ_array = array_sort($activ_array,3,'imagetype');
$j=0;
// first part array
${"activ_array"."$j"} = array();
for($i=0;$i<sizeof($activ_array);$i++){
if($activ_array[$i][3] !=$activ_array[$i-1][3] && $i >1){
$j++;
${"activ_array".$j} = array();
}
array_push (${"activ_array".$j}, $activ_array[$i]);
}
// part array sorting
for($i=0;$i<=$j;$i++){
${"activ_array".$i} = array_sort(${"activ_array".$i},0,'name');
}
$activ_array = array();
for($i=0;$i<=$j;$i++){
$activ_array = array_merge($activ_array,${"activ_array".$i});
}
for($i=0;$i<sizeof($activ_array);$i++){
print_r($activ_array[$i]);
echo("<br>");
}
?>
While searching for a function that would renumber the keys in a array, I found out that array_merge() does this if the second parameter is null:
Starting with array $a like:
<?php
Array
(
[5] => 5
[4] => 4
[2] => 2
[9] => 9
)
?>
Then use array_merge() like this:
<?php
$a = array_merge($a, null);
?>
Now the $a array has bee renumbered, but maintaining the order:
<?php
Array
(
[0] => 5
[1] => 4
[2] => 2
[3] => 9
)
?>
Hope this helps someone :-)
In all PHP 4 versions the above function deals gracefully with paramaters which are NOT arrays(). So if you pass a string or number it will be automagically converted to an one-element array as described in the manual section about Types and "implicit typecasting". So if you ever happen to put an empty/unset variable (NULL value) as parameter, array_merge() will still collapse the other arrays as intended.
From PHP5beta2 on this behaviour changed, and PHP will correctly issue a E_WARNING message, whenever one of the paramters isn't an array. So if you ever happen to be uncertain about the contents of a variable passed as parameter to array_merge() you should use explicit typecasting:
<?php
$a = array(1, 2, 3);
$b = 4; #-- beware: this is no array!
$result = array_merge( (array)$a, (array)$b, ... );
?>
This way $result still would get == array(1, 2, 3, 4); where otherwise the parameters would get rejected with the warning message telling you so.
I got tripped up for a few days when I tried to merge a (previously serialized) array into a object. If it doesn't make sense, think about it... To someone fairly new, it could... Anyway, here is what I did:
(It's obviously not recursive, but easy to make that way)
<?php
function array_object_merge(&$object, $array) {
foreach ($array as $key => $value)
$object->{$key} = $value;
}
?>
Simple problem, but concevibly easy to get stuck on.
I, too have found array_merge_clobber() useful. I use it in a loop to append data to an array. However, when the loop first runs, there will be an error message displayed as array one is empty, so, here is a little fix:
<?php
function array_merge_clobber($arrOne,$arrTwo) {
if(!is_array($arrOne) || !is_array($arrTwo)) return false;
$arrNewArray = $arrOne;
forEach ($arrTwo as $key => $val) {
if (isSet($arrNewArray[$key])) {
if (is_array($val) && is_array($arrNewArray[$key])) {
$arrNewArray[$key] = array_merge_clobber($arrNewArray[$key], $val);
}
else {
$arrNewArray[$key] = $val;
}
}
else {
$arrNewArray[$key] = $val;
}
}
return $arrNewArray;
}
?>
Not sure if someone else has already got something similar or better, but this is simple and works for me. :)
For those who are getting duplicate entries when using this function, there is a very easy solution:
wrap array_unique() around array_merge()
cheers,
k.
This function merges any number of arrays and maintains the keys:
<?php
function array_kmerge ($array) {
reset($array);
while ($tmp = each($array))
{
if(count($tmp['value']) > 0)
{
$k[$tmp['key']] = array_keys($tmp['value']);
$v[$tmp['key']] = array_values($tmp['value']);
}
}
while($tmp = each($k))
{
for ($i = $start; $i < $start+count($tmp['value']); $i ++)$r[$tmp['value'][$i-$start]] = $v[$tmp['key']][$i-$start];
$start = count($tmp['value']);
}
return $r;
}
?>