Combine two 2d arrays and have duplicate rows removed

You can do this $array1 = Array( 0 => Array(“id” => “0001”,”name” => “sample name 1”), 1 => Array(“id” => “0002”,”name” => “sample name 2”), 3 => Array(“id” => “0003”,”name” => “sample name 3”)); $array2 = Array( 0 => Array(“id” => “0002”,”name” => “sample name 2”), 1 => Array(“id” => “11323”,”name” => “blah blah”)); $output … Read more

Insert elements from one array (one-at-a-time) after every second element of another array (un-even zippering)

This example will work regardless of the $a and $b array size. <?php $a = [‘A1’, ‘A2’, ‘A3’, ‘A4’, ‘A5’]; $b = [‘BB1’, ‘BB2’, ‘BB3’, ‘BB4’, ‘BB5’]; for ($i = 0; $i < count($b); $i++) { array_splice($a, ($i+1)*2+$i, 0, $b[$i]); } echo “<pre>” . print_r($a, true) . “</pre>”; Output of this example is : Array … Read more

Array_merge versus + [duplicate]

Because both arrays are numerically-indexed, only the values in the first array will be used. The + operator returns the right-hand array appended to the left-hand array; for keys that exist in both arrays, the elements from the left-hand array will be used, and the matching elements from the right-hand array will be ignored. http://php.net/manual/en/language.operators.array.php … Read more

PHP – How to merge arrays inside array

array_merge can take variable number of arguments, so with a little call_user_func_array trickery you can pass your $result array to it: $merged = call_user_func_array(‘array_merge’, $result); This basically run like if you would have typed: $merged = array_merge($result[0], $result[1], …. $result[n]); Update: Now with 5.6, we have the … operator to unpack arrays to arguments, so … Read more