duandeng7132 2012-05-11 13:09
浏览 20
已采纳

在PHP中搜索多维数组

How can I search the following multidimensional array for the string 'uk'?

array(43)
{
  [0]=> array(1) { ["country"]=> string(9) "Australia" }
  [1]=> array(1) { ["country"]=> string(7) "Bahrain" }
  [2]=> array(1) { ["country"]=> string(7) "Belgium" }
  [3]=> array(1) { ["country"]=> string(8) "Bulgaria" }
  [4]=> array(1) { ["country"]=> string(6) "Canada" }
  [5]=> array(1) { ["country"]=> string(7) "Croatia" }
  [6]=> array(1) { ["country"]=> string(6) "Cyprus" }
  [7]=> array(1) { ["country"]=> string(14) "Czech Republic" }
  [8]=> array(1) { ["country"]=> string(7) "Denmark" }
  [9]=> array(1) { ["country"]=> string(7) "Finland" }
  [10]=> array(1) { ["country"]=> string(7) "Germany" }
  [11]=> array(1) { ["country"]=> string(6) "Greece" }
  [12]=> array(1) { ["country"]=> string(9) "Hong Kong" }
  [13]=> array(1) { ["country"]=> string(7) "Hungary" }
  [14]=> array(1) { ["country"]=> string(7) "Iceland" }
  [15]=> array(1) { ["country"]=> string(5) "India" }
  [16]=> array(1) { ["country"]=> string(4) "Iran" }
  [17]=> array(1) { ["country"]=> string(6) "Israel" }
  [18]=> array(1) { ["country"]=> string(5) "Italy" }
  [19]=> array(1) { ["country"]=> string(5) "Japan" }
  [20]=> array(1) { ["country"]=> string(5) "Korea" }
  [21]=> array(1) { ["country"]=> string(6) "Kuwait" }
  [22]=> array(1) { ["country"]=> string(5) "Malta" }
  [23]=> array(1) { ["country"]=> string(8) "Mongolia" }
  [24]=> array(1) { ["country"]=> string(11) "Netherlands" }
  [25]=> array(1) { ["country"]=> string(6) "Norway" }
  [26]=> array(1) { ["country"]=> string(8) "Pakistan" }
  [27]=> array(1) { ["country"]=> string(6) "Poland" }
  [28]=> array(1) { ["country"]=> string(7) "Romania" }
  [29]=> array(1) { ["country"]=> string(6) "Russia" }
  [30]=> array(1) { ["country"]=> string(9) "Singapore" }
  [31]=> array(1) { ["country"]=> string(8) "Slovakia" }
  [32]=> array(1) { ["country"]=> string(12) "South Africa" }
  [33]=> array(1) { ["country"]=> string(13) "South America" }
  [34]=> array(1) { ["country"]=> string(5) "Spain" }
  [35]=> array(1) { ["country"]=> string(6) "Sweden" }
  [36]=> array(1) { ["country"]=> string(11) "Switzerland" }
  [37]=> array(1) { ["country"]=> string(6) "Taiwan" }
  [38]=> array(1) { ["country"]=> string(10) "Tajikistan" }
  [39]=> array(1) { ["country"]=> string(2) "UK" }
  [40]=> array(1) { ["country"]=> string(20) "United Arab Emirates" }
  [41]=> array(1) { ["country"]=> string(24) "United States of America" }
  [42]=> array(1) { ["country"]=> string(7) "Vietnam" }
}
  • 写回答

2条回答 默认 最新

  • duanbai1370 2012-05-11 13:23
    关注

    1) Why are you encapsulating all your simple strings in a new array?

    1a) If there actually is a reason:

    function my_search($needle, $haystack)
    {
        foreach($haystack as $k => $v)
        {
            if ($v["country"] == $needle)
                return $k;
        }
        return FALSE;
    }
    
    $key = my_search("uk", $yourArray);
    if($key !== FALSE)
    {
        ...
    } else {
        echo "Not Found.";
    }
    

    This function may return Boolean FALSE, but may also return a non-Boolean value which evaluates to FALSE. Please read the section on Booleans for more information. Use the === operator for testing the return value of this function.

    1b) if there isn't:

    2) Fix your array using simple strings as values, not array(1)'s of strings

    3) If it's fixed, you can simply use array_search()

    Edit: Changed arguments to resemble array_search more.

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论
查看更多回答(1条)

报告相同问题?

悬赏问题

  • ¥15 不是,这到底错哪儿了😭
  • ¥15 2020长安杯与连接网探
  • ¥15 关于#matlab#的问题:在模糊控制器中选出线路信息,在simulink中根据线路信息生成速度时间目标曲线(初速度为20m/s,15秒后减为0的速度时间图像)我想问线路信息是什么
  • ¥15 banner广告展示设置多少时间不怎么会消耗用户价值
  • ¥16 mybatis的代理对象无法通过@Autowired装填
  • ¥15 可见光定位matlab仿真
  • ¥15 arduino 四自由度机械臂
  • ¥15 wordpress 产品图片 GIF 没法显示
  • ¥15 求三国群英传pl国战时间的修改方法
  • ¥15 matlab代码代写,需写出详细代码,代价私