doukang2003 2013-01-15 17:02
浏览 49
已采纳

找到匹配值的数组键

I try to get the array key of matching values. It looks like that:

$someId = 2

$array[0][id]   = "1";
$array[0][firstname]  = "dude1";

$array[1][id]   = "2";
$array[1][firstname]  = "dude2";

$array[2][id]   = "3";
$array[2][firstname]  = "dude3";

How I get the array key e.g. "1" ($array[1]), by matching the var "$someId = 2" with the unique IDs ( $array[1][id]) in the array?

Basically: $someId === $array[x][id] > returns the array $array[x] where it matches.

  • 写回答

2条回答 默认 最新

  • dreamy1992 2013-01-15 17:07
    关注

    A simple foreach will do it:

    $someId = 2;
    
    foreach($array as $person)
    {
        if($person['id'] == $someId)
        {
            // found a match, do something with $person
            // ...
            break; // remove the break if you want to continue searching after a match
        }
    }
    

    If you want the key then change to

    foreach($array as $key => $person)
    
    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论
查看更多回答(1条)

报告相同问题?

悬赏问题

  • ¥15 Python时间序列如何拟合疏系数模型
  • ¥15 求学软件的前人们指明方向🥺
  • ¥50 如何增强飞上天的树莓派的热点信号强度,以使得笔记本可以在地面实现远程桌面连接
  • ¥15 MCNP里如何定义多个源?
  • ¥20 双层网络上信息-疾病传播
  • ¥50 paddlepaddle pinn
  • ¥20 idea运行测试代码报错问题
  • ¥15 网络监控:网络故障告警通知
  • ¥15 django项目运行报编码错误
  • ¥15 STM32驱动继电器