php - 合并3个或更多数组而不替换相同的键交替值

时间:2016-12-23 19:29:57

标签: php arrays merge array-merge

我需要合并3个具有相同键的数组。结果必须是一个数组包含3个数组的交替值。

实施例

$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")
);

-------------------------------------结果ARRAY必须是备用的。

$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")
);

怎么能实现这个目标?让最后一个数组以另一种方式添加每个数组?

---------------------更新

我尝试过以下操作:

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

但是,当另一个的索引完成时,结果会给我空数组。

3 个答案:

答案 0 :(得分:1)

$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];
}

答案 1 :(得分:1)

我一直在玩,并提出了一个灵活的解决方案,可以根据需要使用不同数量的项目:

// 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];    
        }
    }
}

如果你有任何额外的数组,只需将它们添加到$groupedArrays数组中,它们就会得到处理。

希望这有帮助!

答案 2 :(得分:0)

为什么不使用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",
     ],
   ]

使用时,请特别注意手册中的以下注释:

  

如果输入数组具有相同的字符串键,则后面的值   该密钥将覆盖前一个密钥。但是,如果是数组   包含数字键,后面的值不会覆盖原始值   值,但会附加。

     

带有数字键的输入数组中的值将重新编号   在结果数组中从零开始递增键。