我有一個多維數組,我正在嘗試使用,這裏是它的樣子。多維數組不能返回正確的值
$states = array(
"California" => array(
"state" => "California",
"abbr" => "CA",
"city" => "Sacramento",
"county" => "Sacramento",
"zip" => "95632"
),
"Washington" => array(
"state" => "Washington",
"abbr" => "WA",
"city" => "Seattle",
"county" => "King",
"zip" => "98101"
),
"Texas" => array(
"state" => "Texas",
"abbr" => "TX",
"city" => "San Antonio",
"county" => "Bexar",
"zip" => "78251"
),
"Florida" => array(
"state" => "Florida",
"abbr" => "FL",
"city" => "Orlando",
"county" => "Orange",
"zip" => "32801"
),
);
當運行foreach循環以從陣列的第一級得到的鑰匙我得到的
California Washington Texas Florida
預期輸出。然而我需要訪問所述陣列的第二層。例如,我需要加州[ '簡稱']所以這是我運行代碼:
foreach (array_keys($states) as $state) {
echo $state['abbr'];
}
,而不是領
CA WA TX FL
就像我希望我得到
C W T F
關於我在做什麼的任何想法都是錯誤的?
雖然此代碼段可以解決的問題,[包括一個解釋](// meta.stackexchange.com/questions/114762/explaining-entirely-code-based-answers)確實有助於提高質量您的帖子。請記住,您將來會爲讀者回答問題,而這些人可能不知道您的代碼建議的原因。也請儘量不要使用解釋性註釋來擠佔代碼,因爲這會降低代碼和解釋的可讀性! – FrankerZ