Matt Elhotiby
Matt Elhotiby

Reputation: 44066

PHP array_merge not doing exactly what i need it to do

Ok so here is my first array:

(
[1] => Array
    (
        [27] => Array
            (
                [product_id] => 27
                [type] => hardware
                [step_number] => 1

            )

        [372] => Array
            (
                [product_id] => 372
                [type] => hardware
                [step_number] => 1

            )

        [92] => Array
            (
                [product_id] => 92
                [type] => hardware
                [step_number] => 1
            )

    )

[2] => Array
    (
        [335] => Array
            (
                [product_id] => 335
                [type] => hardware
                [step_number] => 2

            )

        [62] => Array
            (
                [product_id] => 62
                [type] => hardware
                [step_number] => 2
            )

        [356] => Array
            (
                [product_id] => 356
                [type] => hardware
                [step_number] => 2
            )

    )

and here is my second array

(
[1] => Array
    (
        [655] => Array
            (
                [product_id] => 655
                [type] => optional
                [step_number] => 1
            )

        [54] => Array
            (
                [product_id] => 54
                [type] => optional
                [step_number] => 1
            )

        [554] => Array
            (
                [product_id] => 554
                [type] => optional
                [step_number] => 1
            )
    )

[2] => Array
    (
        [33] => Array
            (
                [product_id] => 33
                [type] => optional
                [step_number] => 2
            )
        [612] => Array
            (
                [product_id] => 612
                [type] => optional
                [step_number] => 2
            )
        [5] => Array
            (
                [product_id] => 5
                [type] => optional
                [step_number] => 2
            )
    ) 

 [3] => Array
            (
                [444] => Array
                    (
                        [product_id] => 444
                        [type] => optional
                        [step_number] => 3
                    )
                [6] => Array
                    (
                        [product_id] => 6
                        [type] => optional
                        [step_number] => 3
                    )
                [53] => Array
                    (
                        [product_id] => 53
                        [type] => optional
                        [step_number] => 3
                    )
            )

Basically what i need is the second array appended to the end of the first array with the keys of the first array preserved the keys and changing the step_number to the new key so the final keys looks like

(
[1] => Array
(
        [27] => Array
            (
                [step_number] => 1)
....
[2] => Array
(
        [335] => Array
            (
                [step_number] => 2)
....
[3] => Array
(
        [655] => Array
            (
                [step_number] => 3)
....
[4] => Array
(
        [33] => Array
            (
                [step_number] => 4)
....
[5] => Array
(
        [444] => Array
            (
                [step_number] => 5)
....

but when i do

$all = array_merge($first, $second);

the keys are

(
[0] => Array
[1] => Array
[2] => Array
[3] => Array
[4] => Array

Is that possible to do....

Upvotes: 0

Views: 125

Answers (3)

adlawson
adlawson

Reputation: 6431

Iterating over the first set of keys, then using the Union array operator should do the trick.

//$first = array(...);
//$second = array(...);

foreach ($second as $key => $value)
{
    if (!isset($first[$key]))
    {
        $first[$key] = array();
    }

    $first[$key] += $value;
}

Upvotes: 0

DaOgre
DaOgre

Reputation: 2100

This is also from the manual on array_splice if you're just looking to append your 2nd array onto the first (http://php.net/manual/en/function.array-push.php):

 array_splice($first, count($first), 0, $second);

Upvotes: 0

user862010
user862010

Reputation:

Try array_merge_recursive .. http://php.net/array_merge_recursive

Upvotes: 2

Related Questions