duancan1950 2015-10-09 03:23
浏览 63
已采纳

使用PHP合并多个关联数组

Recently I've come across a question about merging multiple associative arrays into one.

Following is part of my code

//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";

I have tried grouping them up using array_merge_recursive inside a loop like the following

$arr9 = array();
for( $i = 1 ; $i < 9 ; $i++ ) {
    $arr9 = array_merge_recursive( $arr9 , ${"arr$i"} );
}

The $arr9 outputs as following:

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 ) ) )

I expected the result to be:

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 ) ) ) )

Is there any way to produce the expected output as above? Any help is appreciated!

Thank you for reading this question.

  • 写回答

1条回答 默认 最新

  • doujiu7680 2015-10-09 05:21
    关注

    Here is the code, it uses 6 loops.This is because of array structure. But nothing is hardcoded, if required, its scalable.

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

    output is

    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
                )
    
        )
    
    )
    
    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?

悬赏问题

  • ¥15 乌班图ip地址配置及远程SSH
  • ¥15 怎么让点阵屏显示静态爱心,用keiluVision5写出让点阵屏显示静态爱心的代码,越快越好
  • ¥15 PSPICE制作一个加法器
  • ¥15 javaweb项目无法正常跳转
  • ¥15 VMBox虚拟机无法访问
  • ¥15 skd显示找不到头文件
  • ¥15 机器视觉中图片中长度与真实长度的关系
  • ¥15 fastreport table 怎么只让每页的最下面和最顶部有横线
  • ¥15 R语言卸载之后无法重装,显示电脑存在下载某些较大二进制文件行为,怎么办
  • ¥15 java 的protected权限 ,问题在注释里