douhui3305 2017-08-03 08:47
浏览 60

如何在二维数组中找到值

In my project I have array and when I echo it using pre tag it display like this.

Array
(
[0] => stdClass Object
    (
        [minute] => 1,31
        [hour] => 0-5
        [mday] => *
        [month] => *
        [wday] => *
        [who] => root
        [command] => /usr/bin/nice -n20 adjkerntz -a
    )

[1] => stdClass Object
    (
        [minute] => 1
        [hour] => 3
        [mday] => 1
        [month] => *
        [wday] => *
        [who] => root
        [command] => /usr/bin/nice -n20 /etc/rc.update_bogons.sh
    )

[2] => stdClass Object
    (
        [minute] => */60
        [hour] => *
        [mday] => *
        [month] => *
        [wday] => *
        [who] => root
        [command] => /usr/bin/nice -n20 /usr/local/sbin/expiretable -v -t 3600 sshlockout
    )

[3] => stdClass Object
    (
        [minute] => */60
        [hour] => *
        [mday] => *
        [month] => *
        [wday] => *
        [who] => root
        [command] => /usr/bin/nice -n20 /usr/local/sbin/expiretable -v -t 3600 webConfiguratorlockout
    )

[4] => stdClass Object
    (
        [minute] => 1
        [hour] => 1
        [mday] => *
        [month] => *
        [wday] => *
        [who] => root
        [command] => /usr/bin/nice -n20 /etc/rc.dyndns.update
    )

[5] => stdClass Object
    (
        [minute] => */60
        [hour] => *
        [mday] => *
        [month] => *
        [wday] => *
        [who] => root
        [command] => /usr/bin/nice -n20 /usr/local/sbin/expiretable -v -t 3600 virusprot
    )

[6] => stdClass Object
    (
        [minute] => 30
        [hour] => 12
        [mday] => *
        [month] => *
        [wday] => *
        [who] => root
        [command] => /usr/bin/nice -n20 /etc/rc.update_urltables
    )

[7] => stdClass Object
    (
        [minute] => */1
        [hour] => *
        [mday] => *
        [month] => *
        [wday] => *
        [who] => root
        [command] => /usr/local/www/checksum.php
    )

)

Now I want to check if command=/usr/local/www/checksum.php is exist or not. so, I made this code

if (!in_array('/usr/local/www/checksum.php', $array['command'])) {
    echo 'not';
}
else{
    echo 'yes';
}

But it always echo not...and warning displayed like Message: Undefined index: command. I don't know how to do it. Please help me.

  • 写回答

4条回答 默认 最新

  • dsfdf854456 2017-08-03 08:51
    关注

    Your array is found inside an object, so you must access the object and then the array.

    You can do something like this,

    if (!in_array('/usr/local/www/checksum.php', $arr[0]->obj['command'])) {
        echo 'not';
    }else{
        echo 'yes';
    }
    

    Note, the [0] in this case means the first object inside your array, so you are trying to check if command exists in all of your arrays, you will have to iterate through them like this:

    foreach($arr as $key => $obj){
    
        var_dump(in_array('/usr/local/www/checksum.php', $obj[$key]['command'])); 
    
    }
    
    评论

报告相同问题?

悬赏问题

  • ¥15 Vue3 大型图片数据拖动排序
  • ¥15 划分vlan后不通了
  • ¥15 GDI处理通道视频时总是带有白色锯齿
  • ¥20 用雷电模拟器安装百达屋apk一直闪退
  • ¥15 算能科技20240506咨询(拒绝大模型回答)
  • ¥15 自适应 AR 模型 参数估计Matlab程序
  • ¥100 角动量包络面如何用MATLAB绘制
  • ¥15 merge函数占用内存过大
  • ¥15 使用EMD去噪处理RML2016数据集时候的原理
  • ¥15 神经网络预测均方误差很小 但是图像上看着差别太大