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

我知道這種方式編碼數組中的數組,但我怎麼可以使用它[]呢?如何在PHP中創建嵌套數組?

例如:

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

我想使[ ]內的另一個數組,這可能嗎? (不使用array()

+0

'[]'是用於在PHP 5.4引入陣列的短符號的語法。閱讀更多關於[PHP數組](http://php.net/manual/en/language.types.array.php) – axiac

回答

2

使用:

<?php 
    $myarr=[ 
     "name"=>"sami", 
     "family"=>"soheili", 
     "details" => [ 
      "telephone" => "072...", 
      "email" => "[email protected]" 
     ] 
    ]; 
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) 

?>