我創建了一個具有兩個變量$ state和$ sectors的腳本。如果國家在該部門,我想顯示該部門。下面的示例使用「亞利桑那」作爲國家,它應該輸出部門「西南」,但我不認爲我與陣列正常工作:如果存在子數組中的值,則顯示父鍵
<?php
$state = 'Arizona';
$sectors = array(
"Southeast" => array(
'name' => 'Southeast',
'states' => array('Alabama', 'Georgia', 'Florida', 'South Carolina', 'North Carolina', 'Louisiana', 'Tennessee', 'Kentucky', 'West Virginia', 'Mississippi')
),
"Southwest" => array(
'name' => 'Southwest',
'states' => array('California', 'Arizona', 'New Mexico', 'Utah')
)
);
function has_recursive($sectors, $state)
{
foreach ($state as $key => $value) {
if (!isset($sectors[$key])) {
return false;
}
if (is_array($sectors[$key]) && false === has_recursive($sectors[$key], $value)) {
return false;
}
}
return true;
}
if (has_recursive($sectors, $state) == true){
// false or true
echo $sector['name']; // Displays Southwest
}
請幫助。
你肯定是說' '狀態'=>陣列( '加州', '亞利桑那', '新墨西哥', '爵士')' ? (注意'array(...)') – bwoebi
是否更好做'東南部'=>數組('Alabama','Georgia','Florida','South Carolina','North Carolina','Louisiana ','田納西州','肯塔基州','西弗吉尼亞州','密西西比州')'我如何檢索$部門 – Rizzo
@Rizzo是 – bwoebi