如何从 PHP 中的数组中获取数据

How to fetch data from an array in PHP

如何从 PHP 中的数组获取数据。

我知道如何使用 array_filterarray_search,但我不知道如何以不同的方式搜索数组值中存在的内容。

为了更清楚 --

Suppose i am looking for **Samsung Galaxy S** from my array $data,
but it will return an empty array, because there is not exact match for that data type.

但是"Samsung Galaxy S"数组值1、2和4

中可用

那么我如何从这个数组中获取 "Samsung Galaxy S" !!

任何人都知道如何解决这个问题!!!

$data =
Array
(
    [0] => Array
        (
            [name] => Samsung GT-N7100 Galaxy Note II 16GB
        )

    [1] => Array
        (
            [name] => Samsung GT-i9100 Galaxy S II
        )

    [2] => Array
        (
            [name] => Samsung GT-i9300 Galaxy S III 16GB
        )

    [3] => Array
        (
            [name] => Apple iPhone 5 16GB
        )

    [4] => Array
        (
            [name] => Samsung GT-P5110 Galaxy S 4 10.1 16GB
        )

    [5] => Array
        (
            [name] => Samsung UE46ES6715
        )

    [6] => Array
        (
            [name] => Samsung 830 Series MZ-7PC128 128GB
        )

    [7] => Array
        (
            [name] => Samsung GT-N8000 Galaxy Note 10.1 16GB
        )

    [8] => Array
        (
            [name] => Samsung 830 Series MZ-7PC256 256GB
        )

    [9] => Array
        (
            [name] => Samsung UE46ES6715
        )

    [10] => Array
        (
            [name] => Samsung GT-2423 Galaxy Tab 4 10.1 16GB
        )

您可以循环数组并使用 regexpositive lookahead 来检查模型是否存在:

foreach($models as $key => $model) {
    if (preg_match('/Samsung(?=.*?Galaxy S)/i', $model)) {
        echo $model;
    }
}

正则表达式解释

Samsung(?=.*?Galaxy S)

Options: Case insensitive; Exact spacing; Dot doesn’t match line breaks; ^$ don’t match at line breaks; Greedy quantifiers

Match the character string “Samsung” literally «Samsung»
Assert that the regex below can be matched, starting at this position (positive lookahead) «(?=.*?Galaxy S)»
   Match any single character that is NOT a line break character «.*?»
      Between zero and unlimited times, as few times as possible, expanding as needed (lazy) «*?»
   Match the character string “Galaxy S” literally «Galaxy S»

正则表达式演示:

https://regex101.com/r/oL0vO1/1


PHP 演示:

http://ideone.com/ievsN2

您可以使用 array_filterpreg_match 来完成。我为你的问题做了一个函数 my_array_search :

我将 'Samsung Galaxy S' 中的所有 space (/\s+/) 替换为 .* 以制作正则表达式模式,然后使用 array_filterpreg_match

$data =array('Samsung GT-N7100 Galaxy Note II 16GB','Samsung GT-i9100 Galaxy S II','Samsung GT-i9300 Galaxy S III 16GB','Apple iPhone 5 16GB','Samsung GT-P5110 Galaxy S 4 10.1 16GB','Samsung UE46ES6715','Samsung 830 Series MZ-7PC128 128GB','Samsung GT-N8000 Galaxy Note 10.1 16GB','Samsung 830 Series MZ-7PC256 256GB','Samsung UE46ES6715','Samsung GT-2423 Galaxy Tab 4 10.1 16GB');
print_r($data);

function my_array_search($array, $string)
{
    $pattern = preg_replace ('/\s+/',' .*',preg_quote($string));
    return array_filter($array, 
    function ($value) use($pattern) {
        return preg_match('/'.$pattern.'/',$value ) == 1;
    });
}

print_r(my_array_search($data,'Samsung Galaxy S'));

结果

Array
(
    [0] => Samsung GT-N7100 Galaxy Note II 16GB
    [1] => Samsung GT-i9100 Galaxy S II
    [2] => Samsung GT-i9300 Galaxy S III 16GB
    [3] => Apple iPhone 5 16GB
    [4] => Samsung GT-P5110 Galaxy S 4 10.1 16GB
    [5] => Samsung UE46ES6715
    [6] => Samsung 830 Series MZ-7PC128 128GB
    [7] => Samsung GT-N8000 Galaxy Note 10.1 16GB
    [8] => Samsung 830 Series MZ-7PC256 256GB
    [9] => Samsung UE46ES6715
    [10] => Samsung GT-2423 Galaxy Tab 4 10.1 16GB
)
Array
(
    [1] => Samsung GT-i9100 Galaxy S II
    [2] => Samsung GT-i9300 Galaxy S III 16GB
    [4] => Samsung GT-P5110 Galaxy S 4 10.1 16GB
)