-1

我有一个二维数组,我想知道如何使用 PHP 搜索它。我的数组是:

Array (
    [0] => Array (
        [Image] => 1.png
        [Manufacturer] => Manu1
        [Model] => Model1
    )
    [1] => Array (
        [Image] => 2.png 
        [Manufacturer] => Manu2
        [Model] => Model2
    ) 
    [2] => Array ( 
        [Image] => 3.png
        [Manufacturer] => Manu3
        [Model] => Model3
    )
) ]

我希望人们能够做的是对制造商和型号的组合值进行“LIKE”搜索。即,如果有人搜索:

Ma = Would return all 
Mo = Would return all 
Manu3 = Would return Manu 3 Model3
Manu2 Mo =Would return Manu 2 Mo'XXXX'
Model1 = Would return Manu 1 Model 1
4

1 回答 1

1

这是来自Jeroen的修复代码:

function searchArray($query, $array) {
    $tmp = array();
    $subq = explode(' ', $query);
    foreach ($array as $key => $val) {
        foreach($subq as $q) {
            if (strpos($val['Manufacturer'], $q) !== 0 || strpos($val['Model'], $q) !== 0) {
                $tmp[$key] = $val;
            }
        }
    }
    return $tmp;
}

print_r($tmp);
于 2012-05-09T15:51:33.980 回答