0

I have an array like this one.

$array = Array(
        [0] => Array
            (
                [name] => Eve Greenhaw
                [id] => 456564765
            )

        [1] => Array
            (
                [name] => Tyrone Hallberg
                [id] => 5467652534
            )

        [2] => Array
            (
                [name] => Julio Torbert
                [id] => 254564564
            )

        [3] => Array
            (
                [name] => John Torbert
                [id] => 5462253455
            )

        [4] => Array
            (
                [name] => John Kimmell
                [id] => 4525462
            )

    )

I want to search through the array and return the name and id. For example if the user searches for 'John', I wants the function to return keys 3, and 4. If the user searches for just 'J', the function should return keys 2,3 and 4.

3
  • 1
    Search this page for the word recursive. php.net/manual/en/function.array-search.php Commented Apr 9, 2011 at 17:30
  • Also, what do you mean by "return keys" - do you want the relevant array entries returned (i.e.: the return is an array containing the matching arrays) or just the numeric keys of the original array? In terms of the search, on what basis should it operate? Should it be a direct match from the beginning of the string, or just match elements that contain the provided characters? Commented Apr 9, 2011 at 17:31
  • thanks for the reply. I'm trying to match the elements that cantain the provided characters. I've tried using array_search but only get it to work by searching the full name and doesnt work if the user just searches part of the name. Commented Apr 9, 2011 at 17:42

2 Answers 2

5

One possibility is array_filter:

array_filter(array $input, callback $callback)

Iterates over each value in the input array passing them to the callback function. If the callback function returns true, the current value from input is returned into the result array. Array keys are preserved.

Each member of $array is itself an array, so you could do something like this (assuming you were searching both the name and the id):

$query = 'whatever';

function single_search($member) {
    global $query;

    $in_name = strpos($member['name'], $query);
    $in_id = strpos($member['id'], $query);

    return is_numeric($in_name) || is_numeric($in_id);
}

$filtered = array_filter($array, 'single_search');

The $filtered array contains all the name/id pairs that contained your query. But if you're only interested in the keys, you can use one more function: array_keys. It can return an array of the keys in $filtered, which would be all the keys that matched the query etc etc etc:

$matched_keys = array_keys($filtered);

Good luck.

Sign up to request clarification or add additional context in comments.

1 Comment

And now that I posted that, Gazler's comment about using array_search is way sexier. :/
1

Iterate the $array in foreach() and use stripo()s or preg_match() to find the matching records and return the keys. Very simple.

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.