如何在二维数组中找到价值

时间:2017-08-03 08:47:22

标签: php arrays

在我的项目中,我有数组,当我使用pre标记回显时,它会显示如下。

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
    )

)

现在我想检查命令= / usr / local / www / checksum.php 是否存在。所以,我制作了这段代码

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

但它始终不回应......并且警告显示为消息:未定义索引:命令。我不知道怎么做。请帮我。

4 个答案:

答案 0 :(得分:0)

您的数组位于对象内部,因此您必须访问该对象,然后然后该数组。

你可以这样做,

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

注意,在这种情况下,[0]表示数组中的第一个对象,因此您要检查所有数组中是否存在command,您必须像这样迭代它们:

foreach($arr as $key => $obj){

    var_dump(in_array('/usr/local/www/checksum.php', $obj[$key]['command'])); 

}

答案 1 :(得分:0)

循环遍历数组并在具有多维数组时检查值

$new_array=array();
foreach ($array as $row){
if($row['command']=="/usr/local/www/checksum.php"){
$new_array[] =$row;
}
}
print_r($new_array);

答案 2 :(得分:0)

您使用的阵列是二维数组。你不能使用这个代码..

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

因为它不包含像[command]这样的密钥。

首先在每个元素的单维(正常数组)中断,然后检查。这是代码。使用对象方法

$result = 'not';
foreach($array as $array_element) {
    if (strstr($array_element->command, '/usr/local/www/checksum.php')) {
        $result = 'yes';
        break;
    }
}
echo $result;

答案 3 :(得分:0)

因为它是stdClass Object所以你需要像$row->command

那样访问这个值
foreach ($array as $row){

   if($row->command=="/usr/local/www/checksum.php")
   {
         echo 'yes';
         break;
   }

}