如何根据值查找数组ID

时间:2018-12-01 19:36:21

标签: php

我在做什么错?如何获得阵列的编号,以便为选定的城市分配合适的推销员?在此示例中,我想获得1,因为Ambler是我要寻找的城市。

    $cities = array (
  0 => array("Aldenville", "Analomink"),
  1 => array("Abington", "Ambler", "Ardmore", "Avondale"),  
  2 => array("Ackermanville", "Albrightsville", "Allens Mills", "Alpha"),
  3 => array("Adamstown", "Alburtis", "Allentown"));   

if (($key = array_search("Ambler", $cities)) === false) {
    echo "Not found";
} else{
    echo $key;
}

1 个答案:

答案 0 :(得分:0)

您需要添加搜索级别。您制作了2D阵列的城市,但正在1D进行搜索。当您搜索特定城市时,您将搜索数组'$ cities'中的每个(子)数组