php - merge 3 or more arrays without replacing same keys alternating values

337 views Asked by At

I need to merge 3 arrays that have identical keys. The result must be one array contains alternating values of the 3 arrays.

Example

$array1 = array(
   array("social"=>"facebook", "id"=>"fewf", "name"=>"bbb"),
   array("social"=>"facebook", "id"=>"fr43", "name"=>"ccc"),
   array("social"=>"facebook", "id"=>"fewf", "name"=>"ddd")
);

$array2 = array(
   array("social"=>"twitter", "id"=>"are5", "name"=>"ddd"),
   array("social"=>"twitter", "id"=>"q23q", "name"=>"eee"),
   array("social"=>"twitter", "id"=>"g55h", "name"=>"off"),
   array("social"=>"twitter", "id"=>"r3r3", "name"=>"bgf"),
   array("social"=>"twitter", "id"=>"f333", "name"=>"1qa")
);

$array3 = array(
   array("social"=>"instagram", "id"=>"bv33", "name"=>"ggg"),
   array("social"=>"instagram", "id"=>"nh44", "name"=>"hhh"),
   array("social"=>"instagram", "id"=>"tt12", "name"=>"iii")
);

------------------------------------- RESULT ARRAY must be alternate.

$array_merged = array(
   array("social"=>"facebook", "id"=>"fewf", "name"=>"bbb"),
   array("social"=>"twitter", "id"=>"are5", "name"=>"ddd"),
   array("social"=>"instagram", "id"=>"bv33", "name"=>"ggg"),
   array("social"=>"facebook", "id"=>"fr43", "name"=>"ccc"),
   array("social"=>"twitter", "id"=>"q23q", "name"=>"eee"),
   array("social"=>"instagram", "id"=>"nh44", "name"=>"hhh"),
   array("social"=>"facebook", "id"=>"fewf", "name"=>"ddd"),
   array("social"=>"twitter", "id"=>"g55h", "name"=>"off"),
   array("social"=>"instagram", "id"=>"tt12", "name"=>"iii"),
   array("social"=>"twitter", "id"=>"r3r3", "name"=>"bgf"),
   array("social"=>"twitter", "id"=>"f333", "name"=>"1qa")
);

How could achieve this? Having the final array adding each array in a alternate way?

--------------------- UPDATE

I have tried doing the following:

$new = array();
for ($i=0; $i < $array2; $i++) {
   $new[] = $array1[$i];
   $new[] = $array2[$i];
   $new[] = $array3[$i];
}

However, the result gives me empty arrays when the index of the other finishes.

3

There are 3 answers

2
Ben Plummer On BEST ANSWER

I've been playing around and have come up with a flexible solution that will work for as many arrays as you want along with varying amounts of items:

// Group arrays in a containing array for processing
$groupedArrays = array($array1, $array2, $array3);

$maxArrayItems = max(array_map(function($array) {
    return count($array);
}, $groupedArrays));

$new = array();

// Loop through the amount of times required for the largest array
for ($i=0; $i < $maxArrayItems; $i++) {

    // Loop through for each array in the group of arrays
    for($j=0; $j < count($groupedArrays); $j++) {

        if(isset($groupedArrays[$j][$i])) {
            $new[] = $groupedArrays[$j][$i];    
        }
    }
}

If you have any extra arrays just add them to the $groupedArrays array and they'll be handled.

Hope this helps!

0
aprogrammer On
$new = array();
$maxval = max(count($array1),count($array2),count($array3));
for ($i=0; $i < $maxval; $i++) {
if(array_key_exists($i, $array1)) $new[] = $array1[$i];
if(array_key_exists($i, $array2)) $new[] = $array2[$i];
if(array_key_exists($i, $array3)) $new[] = $array3[$i];
}
0
vfsoraki On

Why not use array_merge?

>>> $array1 = array(
...    array("social"=>"facebook", "id"=>"fewf", "name"=>"bbb"),
...    array("social"=>"facebook", "id"=>"fr43", "name"=>"ccc"),
...    array("social"=>"facebook", "id"=>"fewf", "name"=>"ddd")
... );
=> [
     [
       "social" => "facebook",
       "id" => "fewf",
       "name" => "bbb",
     ],
     [
       "social" => "facebook",
       "id" => "fr43",
       "name" => "ccc",
     ],
     [
       "social" => "facebook",
       "id" => "fewf",
       "name" => "ddd",
     ],
   ]
>>> 
>>> $array2 = array(
...    array("social"=>"twitter", "id"=>"are5", "name"=>"ddd"),
...    array("social"=>"twitter", "id"=>"q23q", "name"=>"eee"),
...    array("social"=>"twitter", "id"=>"g55h", "name"=>"off"),
...    array("social"=>"twitter", "id"=>"r3r3", "name"=>"bgf"),
...    array("social"=>"twitter", "id"=>"f333", "name"=>"1qa")
... );
=> [
     [
       "social" => "twitter",
       "id" => "are5",
       "name" => "ddd",
     ],
     [
       "social" => "twitter",
       "id" => "q23q",
       "name" => "eee",
     ],
     [
       "social" => "twitter",
       "id" => "g55h",
       "name" => "off",
     ],
     [
       "social" => "twitter",
       "id" => "r3r3",
       "name" => "bgf",
     ],
     [
       "social" => "twitter",
       "id" => "f333",
       "name" => "1qa",
     ],
   ]
>>> 
>>> $array3 = array(
...    array("social"=>"instagram", "id"=>"bv33", "name"=>"ggg"),
...    array("social"=>"instagram", "id"=>"nh44", "name"=>"hhh"),
...    array("social"=>"instagram", "id"=>"tt12", "name"=>"iii")
... );
=> [
     [
       "social" => "instagram",
       "id" => "bv33",
       "name" => "ggg",
     ],
     [
       "social" => "instagram",
       "id" => "nh44",
       "name" => "hhh",
     ],
     [
       "social" => "instagram",
       "id" => "tt12",
       "name" => "iii",
     ],
   ]
>>> array_merge($array1, $array2, $array3)
=> [
     [
       "social" => "facebook",
       "id" => "fewf",
       "name" => "bbb",
     ],
     [
       "social" => "facebook",
       "id" => "fr43",
       "name" => "ccc",
     ],
     [
       "social" => "facebook",
       "id" => "fewf",
       "name" => "ddd",
     ],
     [
       "social" => "twitter",
       "id" => "are5",
       "name" => "ddd",
     ],
     [
       "social" => "twitter",
       "id" => "q23q",
       "name" => "eee",
     ],
     [
       "social" => "twitter",
       "id" => "g55h",
       "name" => "off",
     ],
     [
       "social" => "twitter",
       "id" => "r3r3",
       "name" => "bgf",
     ],
     [
       "social" => "twitter",
       "id" => "f333",
       "name" => "1qa",
     ],
     [
       "social" => "instagram",
       "id" => "bv33",
       "name" => "ggg",
     ],
     [
       "social" => "instagram",
       "id" => "nh44",
       "name" => "hhh",
     ],
     [
       "social" => "instagram",
       "id" => "tt12",
       "name" => "iii",
     ],
   ]

When using, take special care with the following notes from manual:

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 array with numeric keys will be renumbered with incrementing keys starting from zero in the result array.