1

Been struggling with this for the last few hours. Trying to build a search function for an array and then spit out a new one containing all arrays that have a keyword.

Here is the function I am working with, not sure if this is heading in the right direction or not, it's returning arrays within arrays which is not what I intend.

function search_array($array, $needle) {
    $results = array();
    $hasValue = false;

    foreach ($array as $subarray) {
        foreach($subarray as $value){
            if(strpos($value,$needle) !== false)
                $hasValue = true;
        }
        if($hasValue = true)
            $results[] = $array;
    }

    return $results;
}

Array I'm searching is structured as follows:

Array
(
    [0] => Array
        (
            [ID] => 27
            [title] => Steve Jobs
            [balance] => $147
            [paid_1] => $49
            [date_1] => 26 August, 2013
            [paid_2] => $49
            [date_2] => 26 August, 2013
            [paid_3] => $49
            [date_3] => 26 August, 2013
        )

    [1] => Array
        (
            [ID] => 26
            [title] => Bill Gates
            [balance] => $300
            [paid_1] => $100
            [date_1] => 25 August, 2013
            [paid_2] => $100
            [date_2] => 25 August, 2013
            [paid_3] => $100
            [date_3] => 25 August, 2013
        )

)

What I would like to happen is use data entered from a search form and then find any arrays that have the keyword entered. For instance searching for "Bill" would return a new array and any other arrays containing "Bill" structured like this:

Array
(

    [0] => Array
        (
            [ID] => 26
            [title] => Bill Gates
            [balance] => $300
            [paid_1] => $100
            [date_1] => 25 August, 2013
            [paid_2] => $100
            [date_2] => 25 August, 2013
            [paid_3] => $100
            [date_3] => 25 August, 2013
        )

)

I've read some other posts on here, doing something similar, especially this one: How to search by key=>value in a multidimensional array in PHP which seems close, except I would like to only search by a value within a string and not the key as well. Any help is greatly appreciated.

Community
  • 1
  • 1
souporserious
  • 2,079
  • 2
  • 27
  • 47

1 Answers1

2

Your function seems almost fine except:

  • You don't check if the value is a string before calling strpos on it
  • You assign $array in your result instead of $subarray
  • You don't reset the $hasValue variable to false
  • You assign value boolean true to the $hasValue variable in your if statement at the end, instead of comparing to true.

I believe this should work:

function search_array($array, $needle) {
    $results = array();

    foreach ($array as $subarray) {
        $hasValue = false;
        foreach($subarray as $value){
            if(is_string($value) && strpos($value,$needle) !== false)
                $hasValue = true;
        }
        if($hasValue)
            $results[] = $subarray;
    }

    return $results;
}

And another version which saves you from traversing the whole subarray every time:

function search_array($array, $needle) {
    $results = array();

    foreach ($array as $subarray) {
        foreach($subarray as $value){
            if(is_string($value) && strpos($value,$needle) !== false) {
                $results[] = $subarray;
                break;
            }
        }
    }

    return $results;
}

Note that if you are looking for an exact match in one of the values instead of a sub-string, you could use the array_search() function instead:

function search_array($array, $needle) {
    $results = array();

    foreach ($array as $subarray) {
        if (array_search($needle, $subarray, true) !== false) {
            $results[] = $subarray;
        }
    }

    return $results;
}
Thierry
  • 376
  • 1
  • 6