Add items to the array without creating a new level

Asked

Viewed 48 times

0

I have the following routine that generates an array for me:

$tipo = "V";

if($tipo == "V"){
    $addonTypeSuccess = ["size" => 10, "option" => 'classic', "type" => "volume"];
}

$arrayAWSAgregado[] = array(
    "product_alias" => 'teste',
    "months" => 1,
    $addonTypeSuccess
);

$arrayAWS[] = array(
    "product_alias" => 'teste',
    "os" => 'linux',
    "months" => 1,
    "addons" => $arrayAWSAgregado
);

$dataAssinatura = array(
    'email' => '[email protected]', 
    'order_id' => 29292, 
    'products' => $arrayAWS
);

This generates the following array:

Array
(
    [email] => [email protected]
    [order_id] => 29292
    [products] => Array
        (
            [0] => Array
                (
                    [product_alias] => teste
                    [os] => linux
                    [months] => 1
                    [addons] => Array
                        (
                            [0] => Array
                                (
                                    [product_alias] => teste
                                    [months] => 1
                                    [0] => Array
                                        (
                                            [size] => 10
                                            [option] => classic
                                            [type] => volume
                                        )

                                )

                        )

                )

        )

)

I would like the [size],[option] and [type] array not to be placed inside an array. How can I do this? I would like them to be at the same level as [product_alias] and [months].

1 answer

2


Utilize array_merge to merge two or more arrays.

$arrayAWSAgregado[] = array_merge(array(
    "product_alias" => 'teste',
    "months" => 1
), $addonTypeSuccess);
  • PERFECT! Thank you very much! \

Browser other questions tagged

You are not signed in. Login or sign up in order to post.