如何在PHP中创建嵌套数组?

时间:2017-03-03 12:45:08

标签: php arrays

<?php $mypeople=array("age"=>23,"fname"=>"sami","family"=>"soheili","job"=>"Dev", "father" => array("age" => 66
, "name" => "fara"
, "family" => "soheili"
, "job" => "clerk") );  ?>

我知道这种方法可以在数组中编码数组,但我如何使用[]呢?

例如:

<?php $myarr=["name"=>"sami","family"=>"soheili"]

我想在[ ]内创建另一个数组,这可能吗? (不使用array()

2 个答案:

答案 0 :(得分:2)

使用:

    <?php
    $myarr=[
        "name"=>"sami",
        "family"=>"soheili",
        "details" => [
            "telephone" => "072...",
            "email" => "test@yahoo.com"
        ]
    ];

答案 1 :(得分:0)

This can be done dynamically too.  
<?php $arr = ["ar"=>"1","ar2"=>"2"];
    print_r( $arr ); // OutPut Array ( [ar] => 1 [ar2] => 2 )
    $arr["ar"] = ["ar11"=>"11","ar2"=>"2"]; 
    print_r( $arr["ar"] ); //Output Array ( [ar11] => 11 [ar2] => 2 )

?>