如果我有以下代碼:Foreach循環+關聯數組
$employeeAges;
$employeeAges["Lisa"] = "28";
$employeeAges["Jack"] = "16";
$employeeAges["Ryan"] = "35";
$employeeAges["Rachel"] = "46";
$employeeAges["Grace"] = "34";
foreach($employeeAges as $name => $age){
echo "Name: $name, Age: $age <br />";
}
我怎麼能輸出的具體信息?下面是一個錯誤的書面例子:
foreach($employeeAges as $name => $age) {
$selective = $name["Lisa"]->$age;
$secondary = $name["Grace"]->$age;
echo "The person you're looking for is $selective years old. And the other one is $secondary years old.";
}
正如你所看到的,我想只抓住特定$ key的$值。上面的代碼輸出以下錯誤:
Trying to get property of non-object
有人可以幫助這段代碼?不勝感激。
+1爲鏈接上' - >' – Jon
尷尬,謝謝!我只是現在試圖用for循環而不是每個for循環。當它達到格雷斯並輸出她的年齡時,是否有可能做到並斷裂? – Sara
當然,但它擊敗了關聯數組的目的。我會通過如何更新我的答案。 – jli