PHP - 如果对象数组中存在值

时间:2014-04-24 14:02:12

标签: php arrays object

我有一个名为$ filtes的对象数组,如下所示:

Array
(
    [0] => stdClass Object
        (
            [title] => Type
            [alias] => type
            [id] => 14
            [parent_id] => 9
            [subs] => Array
                (
                    [0] => stdClass Object
                        (
                            [title] => car
                            [alias] => car
                            [id] => 15
                            [parent_id] => 14
                        )

                )

        )

    [1] => stdClass Object
        (
            [title] => Model
            [alias] => model
            [id] => 18
            [parent_id] => 9
            [subs] => Array
                (
                    [0] => stdClass Object
                        (
                            [title] => XF
                            [alias] => xf
                            [id] => 19
                            [parent_id] => 18
                        )

                    [1] => stdClass Object
                        (
                            [title] => XJ
                            [alias] => xj
                            [id] => 20
                            [parent_id] => 18
                        )

                    [2] => stdClass Object
                        (
                            [title] => XK
                            [alias] => xk
                            [id] => 21
                            [parent_id] => 18
                        )

                    [3] => stdClass Object
                        (
                            [title] => F-TYPE
                            [alias] => f-type
                            [id] => 22
                            [parent_id] => 18
                        )

                )

        )

    [2] => stdClass Object
        (
            [title] => Condition
            [alias] => condition
            [id] => 24
            [parent_id] => 9
            [subs] => Array
                (
                    [0] => stdClass Object
                        (
                            [title] => new
                            [alias] => new
                            [id] => 24
                            [parent_id] => 9
                        )

                )

        )

)

检查$ filters [$ i] - > title和$ filters [$ i] - > subs [$ j] - > title中是否存在单词的最佳做法是什么?速度在这次检查中非常重要。

3 个答案:

答案 0 :(得分:3)

创建一个简单的递归函数:

function myArrayContainsWord(array $myArray, $word) {
    foreach ($myArray as $element) {
        if ($element->title == $word || 
            (!empty($myArray['subs']) && myArrayContainsWord($myArray['subs'], $word)) {
            return true;
        }
    }
    return false;
}

然后这样称呼:

if (myArrayContainsWord($filtes, $title)) {
    ...
}

答案 1 :(得分:1)

我刚刚升级了@ zander-rootman的版本,我添加了一个字段名称选项。

<?php
     function recursive_array_search($needle,$haystack, $field) {
          foreach($haystack as $key=>$value) {
                $current_key=$key;
                if($needle==$value->$field() OR (is_array($value->$field() ) && recursive_array_search($needle,$value->$field(),$field ) != false)) {
                      return true;
                }
          }
          return false;
      }

     // Example of use
     $result = recursive_array_search('2', $ressourceInfo->packs(), "idSubscription");
?>

答案 2 :(得分:0)

PHP in_array和/或PHP array_key_exists ...但是你使用的是多维数组,所以我不确定你想搜索多大的数据。

 <?php
function recursive_array_search($needle,$haystack) {
    foreach($haystack as $key=>$value) {
       $current_key=$key;
       if($needle===$value OR (is_array($value) && recursive_array_search($needle,$value) !== false)) {
            return $current_key;
        }
    }
    return false;
}

在PHP文档中找到此函数:http://www.php.net/array_search