Search code examples
phparraysmultidimensional-arraydeclaration

How to declare a new associative element of a specific first level element?


I have an array like this

$myarray = array(
    'data'=> array(
        'age'=>10
    )
);

How to more data to $myarray['data']

$newdata = array('gender' => 'male');

so that I end up with

$myarray = array(
    'data'=> array(
        'age' => 10,
        'gender' => 'male'
    )
);

Solution

  • You can do this;

    $myarray['data']['gender'] = "male";
    

    Here is a working demo : Demo