skahan
2/14/2014 - 4:01 PM

Multidimensional Associative Arrays - crate and add to.

Multidimensional Associative Arrays - crate and add to.

<?php
//Create multidemnsional array (DW give code error because this is a new shorthand style)
$animals = ["Cat"=>["name"=>"Junior","age"=>16],"Dog"=>["name"=>"Puppy","age"=>"Deceased"]];

Array
(
    [Cat] => Array
        (
            [name] => Junior
            [age] => 16
        )

    [Dog] => Array
        (
            [name] => Puppy
            [age] => Deceased
        )

)

//Add to existing sub-array
$animals["Cat"]["color"]="Grey";

Array
(
    [Cat] => Array
        (
            [name] => Junior
            [age] => 16
            [color] => Grey
        )

    [Dog] => Array
        (
            [name] => Puppy
            [age] => Deceased
        )

)

//Add new element
$animals["Lizard"] = [ "name" => "Eric", "age" => "Deceased" ];

Array
(
    [Cat] => Array
        (
            [name] => Junior
            [age] => 16
            [color] => Grey
        )

    [Dog] => Array
        (
            [name] => Puppy
            [age] => Deceased
        )

    [Lizard] => Array
        (
            [name] => Eric
            [age] => Deceased
        )

)
?>