How to merge array of arrays in php?
If you would like to: Show
You can use this function:
Let's see it in action.. here's some data: Simple structure: Pure array of arrays to merge.
Less simple: Array of arrays, but would like to specify the people and ignore the count.
Run it
Results - Both return this:
Extra Fun: If you want to single out one property in an array or object, like "name" from an array of people objects(or associate arrays), you can use this function
The getter is for possibly protected/private objects.
returns (PHP 4, PHP 5, PHP 7, PHP 8) array_merge — Merge one or more arrays Descriptionarray_merge(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. Values in the input arrays with numeric keys will be renumbered with incrementing keys starting from zero in the result array. Parametersarrays Variable list of arrays to merge. Return ValuesReturns the resulting array. If called without any arguments, returns an empty array. Changelog
ExamplesExample #1 array_merge() example
The above example will output: Array ( [color] => green [0] => 2 [1] => 4 [2] => a [3] => b [shape] => trapezoid [4] => 4 ) Example #2 Simple array_merge() example
Don't forget that numeric keys will be renumbered!
If you want to append array elements from the second array to the first array while not overwriting the elements from the first array and not re-indexing, use the
The keys from the first array will be preserved. If an array key exists in both arrays, then the element from the first array will be used and the matching key's element from the second array will be ignored. array(5) { [0]=> string(6) "zero_a" [2]=> string(5) "two_a" [3]=> string(7) "three_a" [1]=> string(5) "one_b" [4]=> string(6) "four_b" } Example #3 array_merge() with non-array types
The above example will output: Array ( [0] => foo [1] => bar ) See Also
Julian Egelstaff ¶ 13 years ago
[0] = "zero"; ChrisM ¶ 9 months ago
$i=0 ; $i<10000000 ; $i++) { Andreas Hofmann ¶ 10 months ago
JoshE ¶ 6 months ago
How can I merge two arrays in PHP?PHP array_merge() Function. Merge two arrays into one array: $a1=array("red","green"); $a2=array("blue","yellow"); ... . Merge two associative arrays into one array: $a1=array("a"=>"red","b"=>"green"); $a2=array("c"=>"blue","b"=>"yellow"); ... . Using only one array parameter with integer keys: $a=array(3=>"red",4=>"green");. How do I merge two arrays together?The concat() method concatenates (joins) two or more arrays. The concat() method returns a new array, containing the joined arrays. The concat() method does not change the existing arrays.
How do you merge multidimensional arrays?Merging a multidimensional array is the same as that of a simple array. It takes two arrays as input and merges them. In a multidimensional array, the index will be assigned in increment order and will be appended after the parent array. Now, let's take the example of an employee who has two addresses.
How can I merge two arrays in PHP without duplicates?You can use the PHP array_unique() function and PHP array_merge() function together to merge two arrays into one array without duplicate values in PHP.
|