2017-05-19 278 views
0

我正在嘗試向這兩個數組中的每一個添加鍵,以便我可以將所有數據作爲一個數組訪問。這是我的。嘗試多個職位堆棧溢出,但到目前爲止一切都沒有澆築這個概念對我來說:PHP數組關聯數組

$result = array(); 
$age_men = array("Peter"=>"35", "Ben"=>"37", "Joe"=>"43"); 
$age_women = array("Sally"=>"32", "Debra"=>"30", "Miranda"=>"40"); 

$result += ["Men" => $age_men]; 
$result += ["Women" => $age_women]; 

print_r($result); 

//desired output 
Array 
(
    [Men] => Array 
     (
      [Peter] => 35 
      [Ben] => 37 
      [Joe] => 43   
     ) 

    [Women] => Array 
     (
      [Sally] => 32 
      [Debra] => 30 
      [Miranda] => 40 

     ) 
) 
+0

$結果[「Men」] = $ age_men; $ result [「Women」] = $ age_women; – JYoThI

+0

問題是什麼?你的代碼正是你想要的:https://3v4l.org/5U4Ok – Rizier123

+0

你的代碼也在工作,那麼問題是什麼? – JYoThI

回答

1

你的代碼工作,另一種方式是

$result["Men"] = $age_men; $result["Women"] = $age_women; 
0

嘗試象下面這樣:

$result["Men"] = $age_men; 
$result["Women"]= $age_women;