duanmeng3573 2015-09-29 13:14
浏览 19
已采纳

在多维数组中搜索值

I want to find values in multidimensional array. I have an array like this:

array(4) { 
    [0]=> array(2) { 
        [0]=> string(3) "840" 
        [1]=> string(3) "841" } 
    [1]=> array(1) { 
        [0]=> string(3) "842" } 
    [2]=> array(4) { 
        [0]=> string(3) "333" 
        [1]=> string(3) "723" 
        [2]=> string(3) "749" 
        [3]=> string(3) "750" } 
    [3]=> array(4) { 
        [0]=> string(3) "248" 
        [1]=> string(3) "268" 
        [2]=> string(3) "269" 
        [3]=> string(3) "270"   } 
}

I have found here this function:

function in_array_r($needle, $haystack, $strict = false) {
    foreach ($haystack as $item) {
        if (($strict ? $item === $needle : $item == $needle) || (is_array($item) && in_array_r($needle, $item, $strict))) {
            return true;
        }
    }

    return false;
}

But this if statement:

if(!$this->in_array_r($id, $myArray) {} 

doesn't search... I really don't know why. What I am doing wrong guys? I have analised my code multiple times and it seems to be ok.

EDIT: I have this:

foreach($koszyk as $Id_produkty => $Ilosc) {
   if(!$this->in_array_r($Id_produkty, $myArray)) {
       // If the Id_produkty variable is not in $myArray I want to skip to the next element in $koszyk
       continue;
   }

   // Here is mySql query and I'm retrieving data depends on $Id_produkty
}

But it seems like the in_array_r function exits the code after it... It doesn't return any value for me.

Wow, when I set error_reporting to E_ALL I got this error: Call to undefined function in_array_r() I have to figure it out why is that

EDIT2: Ok I got this, I had to add $this-> before calling in_array_r function inside.

  • 写回答

2条回答 默认 最新

  • douxue7196 2015-09-29 13:48
    关注

    Ok I got this, I had to add $this-> before calling in_array_r function inside. I have figure it out because I set error_reporting to E_ALL and the error was: Call to undefined function in_array_r()

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

报告相同问题?

悬赏问题

  • ¥15 基于PLC的三轴机械手程序
  • ¥15 多址通信方式的抗噪声性能和系统容量对比
  • ¥15 winform的chart曲线生成时有凸起
  • ¥15 msix packaging tool打包问题
  • ¥15 finalshell节点的搭建代码和那个端口代码教程
  • ¥15 Centos / PETSc / PETGEM
  • ¥15 centos7.9 IPv6端口telnet和端口监控问题
  • ¥20 完全没有学习过GAN,看了CSDN的一篇文章,里面有代码但是完全不知道如何操作
  • ¥15 使用ue5插件narrative时如何切换关卡也保存叙事任务记录
  • ¥20 海浪数据 南海地区海况数据,波浪数据