5

I have an array of objects called $filtes which goes as follows:

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
                        )

                )

        )

)

What is the best practice to check if a word exists in $filters[$i]->title and $filters[$i]->subs[$j]->title. Speed is really important in this checking.

1

3 Answers 3

8

Create a simple recursive function:

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

Then call it like this:

if (myArrayContainsWord($filtes, $title)) {
    ...
}
Sign up to request clarification or add additional context in comments.

2 Comments

could you please take a look at link, I'm kinda lost - Thanks
If you want to look for an ID instead of the title, you only have to change $element->title to $element->id
2

I just upgraded the version of @zander-rootman, i added a field name option.

<?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");
?>

Comments

0

PHP in_array and/or PHP array_key_exists ... You're using a multi dimensional array however, so I'm not sure how extensively you want to search.

 <?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;
}

Found this function in the PHP docs: http://www.php.net/array_search

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.