Question

I have an array of Users and I want to end up with an array where the username or full name contain a certain character ($char). My array looks like this:

Array
(
    [0] => Array
        (
            [User] => Array
                (
                    [id] => 36
                    [username] => JohnS
                    [fullname] => John Smith
                )

        )

    [1] => Array
        (
            [User] => Array
                (
                    [id] => 137
                    [username] => Tim
                    [fullname] => Tim Wilson
                )

        )

)

However, when I run this array filter - I end up with an empty $result array no matter what value $char is --

$result = array_filter($users, 'matches');

function matches($var){
    return stripos($var["User"]["username"].$var["User"]["fullname"], $char) !== false;
}

Any ideas??!

Thanks

Était-ce utile?

La solution

The $char in your matches function is undefined. (If it is a global variable, then you have to add global $char; first.)

Or do like below:

$result = array_filter($users, function ($var) use ($char) {
  return stripos($var["User"]["username"].$var["User"]["fullname"], $char) !== false;
});
Licencié sous: CC-BY-SA avec attribution
Non affilié à StackOverflow
scroll top