ds11111111111111111 2013-11-14 08:58
浏览 8
已采纳

如何使用PHP查明数组中是否存在键值?

I have a question I need to find if the value of key in an array is existed in PHP and get the index value of it.

Example I have this array:

Array
(
    [0] => Array
        (
            [restaurant_id] => 1519
            [new_lat] => 14.63807
            [new_long] => 121.03158
            [date_updated] => 2013-11-14 16:40:34
        )

    [1] => Array
        (
            [restaurant_id] => 1247
            [new_lat] => 14.63877
            [new_long] => 121.03265
            [date_updated] => 2013-11-14 16:48:41
        )

)

I also used this code also:

$key = array_search($data_add['restaurant_id'],$data);

But it doesn't display the searched value

Now I need to fint if restaurant_id '1247' is existed in array? If existed get that index. Meaning get the index 1 because ID 1247 is in index 1.

I used the array_key_exists() but it will find the key not the key value. I hope you can help me thanks.

  • 写回答

1条回答 默认 最新

  • douyeke2695 2013-11-14 09:04
    关注

    Try this:

    $myArray = []; // Array as above
    $target = '1247';
    
    for ($i=0; $i<count($myArray);$i++) {
        if ($myArray[$i]['restaurant_id'] == $target) {
            break;
        }
    }
    // $i contains index of target
    
    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?

悬赏问题

  • ¥15 stata安慰剂检验作图但是真实值不出现在图上
  • ¥15 c程序不知道为什么得不到结果
  • ¥40 复杂的限制性的商函数处理
  • ¥15 程序不包含适用于入口点的静态Main方法
  • ¥15 素材场景中光线烘焙后灯光失效
  • ¥15 请教一下各位,为什么我这个没有实现模拟点击
  • ¥15 执行 virtuoso 命令后,界面没有,cadence 启动不起来
  • ¥50 comfyui下连接animatediff节点生成视频质量非常差的原因
  • ¥20 有关区间dp的问题求解
  • ¥15 多电路系统共用电源的串扰问题