简体   繁体   中英

PHP Adding element to multidimensional associative array (noob)

I have a multidimensional associative array that is made of of animals:

$animals = ["Cat"=>["name"=>"Junior","age"=>16],"Dog"=>["name"=>"Puppy","age"=>"Deceased"]];

I want to add a new animal to it. I know I can do this:

$animals["Lizard"]["name"]="Allen";
$animals["Lizard"]["age"]="Deceased";

But is there a way to do in in one statement, such as

$animals["Lizard"](["name"]="Eric",["age"]=>"Deceased");

Sorry I know this is a really dumb question but I'm a beginner. Thanks.

Just add entire array as an element:

$animals["Lizard"] = [ "name" => "Eric", "age" => "Deceased" ];

or

$animals["Lizard"] = array( "name" => "Eric", "age" => "Deceased" );

Manual (look at example #6).

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM