lasagne
lasagne

Reputation: 641

Merge multidimesional and associative arrays

I'm trying to merge two given Arrays into a new one:

First array:

Array
(
    [0] =>; Array
        (
            [label] => Please Choose
            [value] => default
        )

)

Second Array:

Array
(
    [label] => 14.09.2013 - 27.09.2013 - 3.299 €
    [value] => 14.09.2013 - 27.09.2013
)

I want to generate an arrays which looks like this:

Array
(
    [0] => Array
        (
            [label] => Please Choose
            [value] => 14.09.2013 - 27.09.2013
        ),
    [1] => Array
        (
            [label] => 14.09.2013 - 27.09.2013 - 3.299 €
            [value] => 14.09.2013 - 27.09.2013
        )

)

I tried to merge the arrays:

array_merge($array1,$array2);

Which results in:

Array
(
    [0] => Array
        (
            [label] => Please Choose
            [value] => default
        )

    [label] => 14.09.2013 - 27.09.2013 - 3.299 €
    [value] => 14.09.2013 - 27.09.2013
)

What is the appropriate function for this use-case?

Upvotes: 0

Views: 48

Answers (3)

Robbert
Robbert

Reputation: 6592

You can simply assign the second array to the first one like this

If $array1 is

Array
  (
    [0] =>; Array
    (
        [label] => Please Choose
        [value] => default
    )

  );

and $array2 is

Array  
  (
     [label] => 14.09.2013 - 27.09.2013 - 3.299 €
     [value] => 14.09.2013 - 27.09.2013
  );

then you can make $array2 a part of $array1 using

$array1[] = $array2;

This will cause $array2 to be a new item in $array1.

Upvotes: 0

Prasanth Bendra
Prasanth Bendra

Reputation: 32740

Try this :

array_merge($array1,array($array2));

Upvotes: 0

DevZer0
DevZer0

Reputation: 13535

if you pass in the 2nd array inside another array you should get the desired output

array_merge($array1,array(1 => $array2));

Upvotes: 2

Related Questions