Search code examples
phparraysmergearray-merge

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


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.


Solution

  • 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!