在多维数组中搜索值并返回新数组

问题描述:

在过去的几个小时中一直为此苦苦挣扎.尝试为数组构建搜索功能,然后吐出一个新的包含所有具有关键字的数组的函数.

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
        )

)

我想发生的事情是使用从搜索表单中输入的数据,然后查找输入了关键字的所有数组.例如,搜索"Bill"将返回一个新数组以及包含"Bill"的其他任何数组,其结构如下:

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.

您的功能似乎很好,除了:

Your function seems almost fine except:

  • 在调用strpos之前,您无需检查该值是否为字符串
  • 您在结果中分配了$ array而不是$ subarray
  • 您不会将$ hasValue变量重置为false
  • 最后,在if语句中为$ hasValue变量分配布尔值true,而不是与true进行比较.

我认为这应该可行:

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

请注意,如果要在其中一个值而不是子字符串中查找完全匹配,则可以改用array_search()函数:

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