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 做个有关计算的小程序
  • ¥15 MPI读取tif文件无法正常给各进程分配路径
  • ¥15 如何用MATLAB实现以下三个公式(有相互嵌套)
  • ¥30 关于#算法#的问题:运用EViews第九版本进行一系列计量经济学的时间数列数据回归分析预测问题 求各位帮我解答一下
  • ¥15 setInterval 页面闪烁,怎么解决
  • ¥15 如何让企业微信机器人实现消息汇总整合
  • ¥50 关于#ui#的问题:做yolov8的ui界面出现的问题
  • ¥15 如何用Python爬取各高校教师公开的教育和工作经历
  • ¥15 TLE9879QXA40 电机驱动
  • ¥20 对于工程问题的非线性数学模型进行线性化