0

I need to merge a PHP array, this array has 2 arrays into it named "targetXX", I can have 2 or more. Each target have the same keys, for each key I have an array with 2 values a and b, a is always the same in both targets, but I need to merge both B values like this:

Array
(
    [0] => Array
        (
            [target] => hitcount(stats.asdf1.requests, "1min")
            [datapoints] => Array
                (
                    [0] => Array
                        (
                            [0] => 1200
                            [1] => 1392282200
                        )

                    [1] => Array
                        (
                            [0] => 1400
                            [1] => 1392282260
                        )

                    [2] => Array
                        (
                            [0] => 600
                            [1] => 1392282320
                        )

                    [3] => Array
                        (
                            [0] => 200
                            [1] => 1392282380
                        )

                    [4] => Array
                        (
                            [0] => 400
                            [1] => 1392282440
                        )

                    [5] => Array
                        (
                            [0] => 600
                            [1] => 1392282500
                        )
                )
        )
    [1] => Array
        (
            [target] => hitcount(stats.asdf.requests, "1min")
            [datapoints] => Array
                (
                    [0] => Array
                        (
                            [0] => 4321
                            [1] => 1392282200
                        )

                    [1] => Array
                        (
                            [0] => 76567
                            [1] => 1392282260
                        )

                    [2] => Array
                        (
                            [0] => 5556
                            [1] => 1392282320
                        )

                    [3] => Array
                        (
                            [0] => 7675
                            [1] => 1392282380
                        )

                    [4] => Array
                        (
                            [0] => 2344
                            [1] => 1392282440
                        )

                    [5] => Array
                        (
                            [0] => 0999
                            [1] => 1392282500
                        )
                )
        )

Result:

    Array
(
    [0] => Array
        (
            [target] => hitcount(stats.asdf1.requests, "1min")
            [datapoints] => Array
                (
                    [0] => Array
                        (
                            [0] => 1200
                            [1] => 1392282200
                            [2] => 4321
                        )

                    [1] => Array
                        (
                            [0] => 1400
                            [1] => 1392282260
                            [2] => 76567
                        )

                    [2] => Array
                        (
                            [0] => 600
                            [1] => 1392282320
                            [2] => 5556
                        )

                    [3] => Array
                        (
                            [0] => 200
                            [1] => 1392282380
                            [2] => 7675
                        )

                    [4] => Array
                        (
                            [0] => 400
                            [1] => 1392282440
                            [2] => 2344
                        )

                    [5] => Array
                        (
                            [0] => 600
                            [1] => 1392282500
                            [2] => 0999
                        )
                )
        )
relvira
  • 99
  • 2
  • 2
  • 9
  • I've tried to put each target on a new array and merge those arrays with array_merge_recursive, but i dont know how to solve it because i can have 2 or more targets :( – relvira Feb 13 '14 at 18:22
  • You can just call `array_merge_recursive` repeatedly for until you went over all your sub-arrays. – T0xicCode Feb 13 '14 at 18:24

2 Answers2

2

The built-in function array_merge may do the work for you. You need to merge each subarrays in fact, as the array_merge_recursive function doesn't handle indexes.

$newArray = array();
foreach ($myArray['target2'] as $key => $arr) {
    $newArray['target'][$key] = array_merge($myArray['target1'][$key], $arr[1]);
}

Merges the elements of one or more arrays together so that the values of one are appended to the end of the previous one. It returns the resulting array.

If the input arrays have the same string keys, then the later value for that key will overwrite the previous one. If, however, the arrays contain numeric keys, the later value will not overwrite the original value, but will be appended.

If you have more than 2 keys to merge, you can loop on the algorithm multiple times.

Maxime Lorant
  • 34,607
  • 19
  • 87
  • 97
2

Use array_merge() to achieve this:

$newArray = array();

foreach ($myArray['target2'] as $key => $innerArr1) {
    $newArray['target'][$key] = array_merge(
        $myArray['target1'][$key],  /* 0th and 1st index */
        array($innerArr1[1])        /* 2nd index         */
    );
}

print_r($newArray);

Output:

Array
(
    [target] => Array
        (
            [0] => Array
                (
                    [0] => 333333
                    [1] => 13
                    [2] => 99
                )

            [1] => Array
                (
                    [0] => 444444
                    [1] => 15
                    [2] => 98
                )

            [2] => Array
                (
                    [0] => 555555
                    [1] => 17
                    [2] => 97
                )

        )

)

Demo

Amal Murali
  • 75,622
  • 18
  • 128
  • 150
  • Hello @amal-murali, Finally the Array structure is different, i've tried to implement your solution into it but it just don't work, could you help me with this? Thank you so much!! – relvira Feb 14 '14 at 09:09