php数组从多维数组按单个或多个单词搜索

php数组从多维数组按单个或多个单词搜索

本文介绍了php数组从多维数组按单个或多个单词搜索的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个多维数组.我想在数组中使用单个或两个单词进行搜索,然后返回包含任何匹配单词的数组...

I've a multidimensional array. I want to search in array using single or double word inside it and return the array's that contain any matched word...

这是我的示例数组:

$array = array(
            array('First_name' => 'Dina', 'Last_Name' => 'Gomez', 'Location' => 'Los Angeles, CA', 'Work' => 'Youtube Service')
            array('First_name' => 'Lopa', 'Last_Name' => 'Mitchel', 'Location' => 'New York, NY', 'Work' => 'Works with Mark')
            array('First_name' => 'Mark ', 'Last_Name' => 'Nailson Jr.', 'Location' => 'Dallas, USA', 'Work' => 'SEO Industry')
            array('First_name' => 'Jenna', 'Last_Name' => 'Gomez', 'Location' => 'Florida, CA', 'Work' => 'Work at Youtube')
            );

现在,如果有人搜索"Gomez",它应该返回第一个和最后一个数组.我可以使用 in_array()进行操作,但是问题是,我 in_array()仅返回完全匹配.我也需要部分匹配的内容.例如,如果有人搜索马克·戈麦斯" ,那么它应该返回第一个数组,戈麦斯" 的最后一个数组和第二个&第三个数组为标记" ..第二个数组的 Work Key 上有"Mark" 字.

Now If someone search "Gomez" then it should return 1st and last array..I can do it using in_array() but problem is this, I in_array() only return exact match. I want partial match content too.. like if someone search "Mark Gomez" then it should return 1st array, last array for "Gomez" and 2nd & third array for "Mark" .. 2nd array has "Mark" word at Work Key.

我从另一个stackoverflow答案..中收集了一个函数,该函数仅适用于完全匹配..或仅适用于某些键.

I collect a function from another stackoverflow answer.. which is only work for exact match.. or for some key's only..

function checkarrayvalues($term, $arr, $strict = false, $partial = false) {
        if ($partial) {  // whether it should perform a partial match
            $fn = ($strict) ? "strpos" : "stripos";
        }
        foreach ($arr as $item) {
            if (is_array($item)) {
                if (checkarrayvalues($term, $item, $strict, $partial))
                    return true;
            } elseif (($partial && call_user_func($fn, $item, $term) !== false)
                    || ($strict ? $item === $term : $item == $term)) {
                return true;
            }
        }
        return false;
    }

    var_dump(checkarrayvalues($query, $results, false, true));

我也尝试过 array_search 函数:(不走运.

I also tried array_search function :( No luck.

请帮助:(

推荐答案

您非常亲密.您可以展开输入项,然后分别搜索它们以查找数组中的出现项.然后,您将无法直接返回递归数组的值,因为这会破坏循环.您可以使用变量并在循环结束时将其返回

You were very close. You can explode the input terms and search them separately to find occurrences in the array. Then, you cannot return directly the value of the recursive array, because you'll break the loop. You can use a variable and return it at the end of the loop

这是演示.

功能:

function checkarrayvalues(&$out, $terms, $input, $strict = false, $partial = false) {

    if ($partial) {  // whether it should perform a partial match
        $fn = ($strict) ? "strpos" : "stripos";
    }

    $found = false ;
    foreach ($input as $key => $item) {
        if (is_array($item)) {
            if (checkarrayvalues($out, $terms, $item, $strict, $partial)) {
                $found = true;
            }
        }
        else {
            foreach ($terms as $term) {
                if (($partial && call_user_func($fn, $item, $term) !== false)
                    || ($strict ? $item === $term : $item == $term))
                {
                    $out[] = ['item' => $item, 'key' => $key] ;
                    $found = true;
                }
            }
        }
    }
    return $found;
}

用法:

$array = [
    ['First_name' => 'Dina', 'Last_Name' => 'Gomez', 'Location' => 'Los Angeles, CA', 'Work' => 'Youtube Service'],
    ['First_name' => 'Lopa', 'Last_Name' => 'Mitchel', 'Location' => 'New York, NY', 'Work' => 'Works with Mark'],
    ['First_name' => 'Mark', 'Last_Name' => 'Nailson Jr.', 'Location' => 'Dallas, USA', 'Work' => 'SEO Industry'],
    ['First_name' => 'Jenna', 'Last_Name' => 'Gomez', 'Location' => 'Florida, CA', 'Work' => 'Work at Youtube'],
];


$out = [];
if (checkarrayvalues($out, ["New York"], $array, false, false)) {
    print_r($out);
}
$out = [];
if (checkarrayvalues($out, ["Mark","Gomez"], $array, false, false)) {
    print_r($out);
}
$out = [];
if (checkarrayvalues($out, ["Mark"], $array, false, true)) {
    print_r($out);
}

输出:

Array ( [0] => Array ( [item] => New York, NY [key] => Location ) )
Array ( [0] => Array ( [item] => Gomez [key] => Last_Name ) [1] => Array ( [item] => Mark [key] => First_name ) [2] => Array ( [item] => Gomez [key] => Last_Name ) )
Array ( [0] => Array ( [item] => Works with Mark [key] => Work ) [1] => Array ( [item] => Mark [key] => First_name ) )

当然,您可以存储$ input或索引,而不是存储项目.

Of course, instead of storing the item, you could store the $input, or the index.

这篇关于php数组从多维数组按单个或多个单词搜索的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

09-03 10:32