4

I know how strpos works, and it is working as expected, but the return in the if function isn't.

Some code:

foreach ($socialstream as $key => $social) {
    //filtering in here
    $result= $this->search_objects($social);
    ....
}

My function search_objects:

function search_objects($objects)
{
    $filters = array('word', 'test');
    foreach ($objects as $key => $value) {
        if (is_array($value) || is_object($value)) {
            $this->search_objects($value);
        } else {
            //look for faulty strings in value
            foreach ($filters as $filter) {
                if (!is_int($value) && strpos($value, $filter) !== false) {
                    return true;
                }
            }

        }
    }
    return false;
}

If I print out my $result, I always get false back, instead of the true in the if function. I know it gets to the if when the needle exists in the haystack, by debugging, it's just not returning it.

What am I missing?

halfer
  • 19,824
  • 17
  • 99
  • 186
Bananam00n
  • 814
  • 9
  • 27
  • Add `var_dump(!is_int($value) && strpos($value, $filter) !== false);` inside your second `foreach` and see what it outputs on each iteration. – Amal Murali Dec 20 '13 at 09:54
  • Did you actually read my first comment? "not **a** var_dump()". Copy paste that exact command :) – Amal Murali Dec 20 '13 at 09:57
  • Returns true if it exist, otherwise false. Is this what you expected it to do? – Bananam00n Dec 20 '13 at 10:02
  • @Bananam00n What are you trying to do? If a single filter is found, it should return `false` right? – h2ooooooo Dec 20 '13 at 10:05
  • as soon as 1 word of the filter array exist, it should return true, and get out of the foreach, because there's no point to continue. – Bananam00n Dec 20 '13 at 10:06
  • 1
    Consider adding an `array` type hint to your function for safety, i.e. `function search_objects(array $objects)`. That will kick out anything that isn't an array (presuming that is what you want). – halfer Dec 20 '13 at 10:15

1 Answers1

2

I think your problem has to do with the recursive part:

    if (is_array($value) || is_object($value)) {
        $this->search_objects($value);
    }

You probably want to do something with the return value. Like: if ($this->search_objects($value)) return true;

(Then again, I'm not sure what you are trying to accomplish)

edit: Try this:

function search_objects($objects)
{
    $filters = array('word', 'test');
    foreach ($objects as $key => $value) {
        if (is_array($value) || is_object($value)) {
            if ($this->search_objects($value)) {
                return true;
            }
        } else {
            //look for faulty strings in value
            foreach ($filters as $filter) {
                if (!is_int($value) && strpos($value, $filter) !== false) {
                    return true;
                }
            }

        }
    }
    return false;
}
Tasos Bitsios
  • 2,699
  • 1
  • 16
  • 22
  • the function is to remove posts which have - for example - curse words in them. so if there is a curse word in a post, the whole thing should not be shown. (instead of 'deleting' the word out of the post) – Bananam00n Dec 20 '13 at 10:04
  • 1
    my point is that the recursive bit's result is completely ignored. if the outer function call finds a $value that is array or object, it will call the same function again (inner) with $value but when it returns, the outer function does nothing with the result. So it defaults to the 'return false' at the end of the function. – Tasos Bitsios Dec 20 '13 at 10:07
  • ooh, now I see what you did! thank you so much! :-) Can't believe I missed that. – Bananam00n Dec 20 '13 at 10:14