繁体   English   中英

如何从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

那么,我该如何从该阵列中获取“三星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
        )

您可以循环数组并使用正向查找regex检查模型是否存在:

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做到这array_filter 我为您的问题做了一个函数my_array_search

我将'Samsung Galaxy S'所有空格( /\\s+/ )替换为.*以创建正则表达式,然后使用带有preg_match array_filter

$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
)

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM