PHP array_push without numeric key

how can I push a new array without a numeric key?

$array = array('connect' => array('mydomain.com' => 1.99) );
$new_array['mynewdomain.com'] = 2.99;

array_push($array['connect'], $new_array);

      

Currently returning:

Array
(
    [connect] => Array
        (
            [mydomain.com] => 1.99
            [0] => Array
                (
                    [mynewdomain.com] => 2.99
                )
        )
)

      

https://ideone.com/VgL67Y

I am expecting the following output:

Array
(
    [connect] => Array
        (
            [mydomain.com] => 1.99
            [mynewdomain.com] => 2.99
        )
)

      

+3


source to share


3 answers


Just add the item to the array.

$array['connect']['mynewdomain.com'] = 2.99;

      



No need to do array_push()

. Just use PHP

in built constructs to get the job done.

It is faster in built language constructs than in built-in functions and user-defined functions.

+9


source


Use for this +

. Try with



$array = array('connect' => array('mydomain.com' => 1.99) );
$array['connect'] += array('mynewdomain.com' => 2.99);

      

+6


source


Use array_merge()

:

$array['connect'] = array_merge($array['connect'], $new_array);

      

+5


source







All Articles