dos3018 2014-01-23 07:54
浏览 30
已采纳

在php中检查二维数组的值

I have an array like this:

array(2) { 
          [0]=> array(1) { ["cate_id"]=> string(2) "14" }
          [1]=> array(1) { ["cate_id"]=> string(2) "15" }
         }

How can I check if the value 14 exists in the array without using a for loop?

I've tried this code:

var_dump(in_array('14',$categoriesId));exit;

but it returns false, and I do not know why.

  • 写回答

2条回答 默认 最新

  • douliao7354 2014-01-23 07:58
    关注

    I wonder why you don't need a for. Well a quickest way would be to serialize your array and do a strpos.

    $yourarray = array('200','3012','14');
    if(strpos(serialize($yourarray),14)!==false)
    {
    echo "value exists";
    }
    

    Warning : Without using looping structures you cannot guarantee the value existence inside an array. Even an in_array uses internal looping structures. So as the comments indicate you will get a false positive if there is 1414 inside the $yourarray variable. That's why I made it a point in the first place.

    If you need to find a specific value in an array. You have to loop it.

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

报告相同问题?

悬赏问题

  • ¥15 ad5933的I2C
  • ¥15 请问RTX4060的笔记本电脑可以训练yolov5模型吗?
  • ¥15 数学建模求思路及代码
  • ¥50 silvaco GaN HEMT有栅极场板的击穿电压仿真问题
  • ¥15 谁会P4语言啊,我想请教一下
  • ¥15 哪个tomcat中startup一直一闪而过 找不出问题
  • ¥15 这个怎么改成直流激励源给加热电阻提供5a电流呀
  • ¥50 求解vmware的网络模式问题 别拿AI回答
  • ¥24 EFS加密后,在同一台电脑解密出错,证书界面找不到对应指纹的证书,未备份证书,求在原电脑解密的方法,可行即采纳
  • ¥15 springboot 3.0 实现Security 6.x版本集成