2012-08-23 152 views
-1

這是我的數組;在數組中搜索

$subcategory = array(
    "cata" => array("", "Finance", "Economics", "Accounting", "Operations Management"), 
    "catb" => array("", "Computer Science", "Electrical Engineering", "Mechanical Engineering", "Civil Engineering"), 
    "catc" => array("", "Poetry", "Literature", "Communications"), 
    "catd" => array("", "American History", "European History", "World History") 
    ); 

如果我搜索「詩」,我怎麼能得到回報「CATC」?

+0

這裏有一個提示:'for'環路'for'循環 –

+1

內通過循環陣列上,然後遍歷每個嵌套陣列。這是PHP語法的一個簡單問題,向我們展示您嘗試的內容,我們可以幫助您解決任何問題。 – meagar

回答

0

退房留言給PHP函數array_search,類似的事情已經提到有一起。

0

考慮您的數組的我會用這個解決方案去結構:

$search = "Poetry"; 
    foreach ($subcategory as $categoryName => $values) 
     if (in_array($search, $subcategory[$categoryName])) return $categoryName;