最近我遇到了一个关于将多个关联数组合并为一个的问题。
以下是我的代码的一部分
//The Arrays ( Top 8 from the list )
$arr1["XMLResultColumn"]["1"]["Series"] = "Column1";
$arr2["XMLResultColumn"]["1"]["Series"] = "Column2";
$arr3["XMLResultColumn"]["1"]["Categories"] = "Column3";
$arr4["XMLResultColumn"]["1"]["Values"] = "Column4";
$arr5["XMLResultColumn"]["1"]["SecondaryValues"] = "Column5";
$arr6["XMLResultColumn"]["2"]["Series"] = "Column1";
$arr7["XMLResultColumn"]["2"]["Series"] = "Column2";
$arr8["XMLResultColumn"]["2"]["Categories"] = "Column3";
我尝试使用array_merge_recursive
在循环中对它们进行分组,如下所示
$arr9 = array();
for( $i = 1 ; $i < 9 ; $i++ ) {
$arr9 = array_merge_recursive( $arr9 , ${"arr$i"} );
}
$arr9
输出如下:
Array ( [XMLResultColumn] => Array ( [1] => Array ( [Series] => Column1 ) [2] => Array ( [Series] => Column2 ) [3] => Array ( [Categories] => Column3 ) [4] => Array ( [Values] => Column4 ) [5] => Array ( [SecondaryValues] => Column5 ) [6] => Array ( [Series] => Column1 ) [7] => Array ( [Series] => Column2 ) [8] => Array ( [Categories] => Column3 ) ) )
我预计结果是:
Array ( [XMLResultColumn] => Array ( [1] => Array ( [Series] => Array ( [0] => Column1 [1] => Column2 ) [Categories] => Array ( [0] => Colomn3 ) [Values] => Array ( [0] => Column4 ) [SecondaryValues] => Array ( [0] => Column5 ) ) [2] => Array ( [Series] => Array ( [0] => Column1 [1] => Column2 ) [Categories] => Array ( [0] => Colomn3 ) ) ) )
有没有办法产生如上所述的预期输出? 任何帮助表示赞赏!
感谢您阅读此问题。
答案 0 :(得分:1)
这是代码,它使用6个循环。这是因为数组结构。但是,如果需要,没有任何硬编码可扩展。
$arr1["XMLResultColumn"]["1"]["Series"] = "Column1";
$arr2["XMLResultColumn"]["1"]["Series"] = "Column2";
$arr3["XMLResultColumn"]["1"]["Categories"] = "Column3";
$arr4["XMLResultColumn"]["1"]["Values"] = "Column4";
$arr5["XMLResultColumn"]["1"]["SecondaryValues"] = "Column5";
$arr6["XMLResultColumn"]["2"]["Series"] = "Column1";
$arr7["XMLResultColumn"]["2"]["Series"] = "Column2";
$arr8["XMLResultColumn"]["2"]["Categories"] = "Column3";
$arr9 = $a = $a1 = array();
for ($i = 1; $i < 9; $i++) {
$a[] = implode(",", array_keys(${"arr$i"}["XMLResultColumn"]));
}
$a = array_values(array_unique($a));
foreach ($a as $key => $val) {
for ($i = 1; $i < 9; $i++) {
if (array_key_exists($val, ${"arr$i"}["XMLResultColumn"]))
if (array_key_exists($val, $a1)) {
if (!in_array(implode(",", array_keys(${"arr$i"}["XMLResultColumn"][$val])), $a1[$val]))
$a1[$val][] = implode(",", array_keys(${"arr$i"}["XMLResultColumn"][$val]));
} else
$a1[$val][] = implode(",", array_keys(${"arr$i"}["XMLResultColumn"][$val]));
}
}
foreach ($a1 as $key => $val) {
foreach ($val as $val1) {
for ($i = 1; $i < 9; $i++) {
if (array_key_exists($key, ${"arr$i"}["XMLResultColumn"]))
if (array_key_exists($val1, ${"arr$i"}["XMLResultColumn"][$key]))
$arr9[$key][$val1][] = ${"arr$i"}["XMLResultColumn"][$key][$val1];
}
}
}
print_r($arr9);
输出
Array
(
[1] => Array
(
[Series] => Array
(
[0] => Column1
[1] => Column2
)
[Categories] => Array
(
[0] => Column3
)
[Values] => Array
(
[0] => Column4
)
[SecondaryValues] => Array
(
[0] => Column5
)
)
[2] => Array
(
[Series] => Array
(
[0] => Column1
[1] => Column2
)
[Categories] => Array
(
[0] => Column3
)
)
)