0

我有一个名为 results_array 的数组:

var_dump($results_array):
0 => php,mysql,jquery,ruby,html,css,lamp
1 => mouse,keyboard,laptop,pad
2 => table,sofa,caption
3 => control,television,control television,television control

我有一个$q代表查询,我想在 $results_array 中搜索并删除与查询无关的项目,所以如果我设置$q=a然后结果数组应该是这样的:

0 => lamp
1 => keyboard,laptop,pad
3 => table,sofa,caption
4 => empty

现在,我想把上面的结果放在results_array的每个索引中,最后results_array应该是:

0 => lamp
1 => keyboard
2 => laptop
3 => pad
4 => table
5 => sofa
6 => caption

我的代码是:

foreach($results_array as &$row) {
            $row = explode(',', $row);
        }
        unset($row);
        $results_array = call_user_func_array('array_merge_recursive', $results_array);

        foreach ($array as $k => $v) {
            if(strpos($v, 'a') === false) unset($array[$k]);
        }
        $results_array = array_values($results_array);

这正是我需要的,现在我设置了我的 $q = 'tele co',现在在应用上面的代码集 $ = 'tele co' 之后,它返回 emtpy,但它不应该是因为:

3 => control,television,control television,television control

“控制电视,电视控制”应该返回,但不是,所以我将代码更改为:

foreach($results_array as &$row) {
            $row = explode(',', $row);
        }
        unset($row);
        $results_array = call_user_func_array('array_merge_recursive', $results_array);
// HERE IS CHANGED ****
        $q = preg_split('/\s+/', trim($q));
        foreach ($results_array as $key => $value) {
            foreach ($q as $query) {
                if (stripos($value, $query) === false) {
                    unset($results_array[$key]);
                    break;
                }
            }
        }
// ****
        $results_array = array_values($results_array);

它仍然无法正常工作,通过传递 $q = 'tele co' 我需要返回 'television control' 和 'control TV',因为他们都得到了 $q 中的 'tele' AND 'co'

我感谢任何形式的帮助

4

2 回答 2

1

假设:

$q = 'a c';
$results_array = array(
    'php,mysql,jquery,ruby,html,css,lamp',
    'mouse,keyboard,laptop,pad',
    'table,sofa,caption',
    'control,television,control television,television control'
);

解决方案:

$result = array_map(function ($element) use ($q) {
    $words = explode(',', $element);
    return array_filter($words, function ($word) use ($q) {
        $qwords = explode(' ', $q);
        return count(array_filter($qwords, function ($qword) use ($word) {
            return strpos($word, $qword) !== false;
        })) > 0;
    });
}, $results_array);

输出 print_r($result);

Array
(
    [0] => Array
        (
            [5] => css
            [6] => lamp
        )

    [1] => Array
        (
            [1] => keyboard
            [2] => laptop
            [3] => pad
        )

    [2] => Array
        (
            [0] => table
            [1] => sofa
            [2] => caption
        )

    [3] => Array
        (
            [0] => control
            [2] => control television
            [3] => television control
        )

)
于 2012-11-14T17:51:33.053 回答
1

如果我正确理解您的问题,您正在寻找这个:

$result = array();
$parts = explode( ' ', $q);
foreach($results_array as $words){
     foreach( explode(',', $words) as $word){
         $good = true;

         foreach( $parts as $p){
             if( strpos( $word, $p) === false){
                 $good = false;
                 break;
             }
         }

         if( $good){
            $result[] = $word;
         }
     }
}
于 2012-11-14T17:51:43.800 回答