如何从PHP中的数组中获取数据。
我知道如何使用array_filter
或array_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.
但"三星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
)
答案 0 :(得分:2)
您可以循环数组并使用regex
和positive 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»
REGEX DEMO:
https://regex101.com/r/oL0vO1/1
PHP DEMO:
答案 1 :(得分:0)
您可以使用array_filter
和preg_match
执行此操作。我为你的问题做了一个函数my_array_search
:
我将所有空格(/\s+/
)替换为.*
中的'Samsung Galaxy S'
以制作正则表达式模式,然后将array_filter
与preg_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
)