0

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.

8
  • can you paste, print_r($arr9) instead of var_dump($arr9).?? Commented Oct 9, 2015 at 3:41
  • Question updated , thank you Commented Oct 9, 2015 at 3:55
  • 1
    You're trying to achieve some array_PUSH_recursive result. Can't do that with native function (some hackish callback or nested loops would be needed). I would take step back and see if I can't gather that data correctly in the first place. If you add [] at the end of each assignment and get rid of array numbers (push into the same array) you'll get what you need (example: $arr['xml']['0']['something'][] = 'string1'; $arr['xml']['0']['something'][] = 'string2'; ...) Commented Oct 9, 2015 at 4:44
  • i have manipulated with code without any native functions, if u need i can post the answer. Commented Oct 9, 2015 at 4:57
  • Thanks for shudder for your pretty nice idea, I will give a try to your suggestion and see if it works. Commented Oct 9, 2015 at 4:57

1 Answer 1

1

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
            )

    )

)
Sign up to request clarification or add additional context in comments.

2 Comments

Nice, +1 for your effort. I will try it with my xml pool when I'm back to my office and to see if there is any performance issue.
3 loops are extra because, i have not hard coded, u can add any number of index like 0,1,2,3,4 and for each index any number of sub indexex like series,series1,series2... etc.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.