字符串 $input
=“John的一加二”,
数组 $devices
array:54 [
0 => array:1 [
"name" => "xperia-z-ultra"
]
1 => array:1 [
"name" => "xbox-one"
]
2 => array:1 [
"name" => "xbox-360"
]
3 => array:1 [
"name" => "withings-camera"
]
4 => array:1 [
"name" => "withing"
]
5 => array:1 [
"name" => "win-7"
]
6 => array:1 [
"name" => "surface-pro-4"
]
7 => array:1 [
"name" => "surface-book"
]
8 => array:1 [
"name" => "surface-3"
]
9 => array:1 [
"name" => "roku"
]
10 => array:1 [
"name" => "play-station-4"
]
11 => array:1 [
"name" => "one-plus-2"
]
12 => array:1 [
"name" => "nokia-lumia"
]
13 => array:1 [
"name" => "no-img"
]
14 => array:1 [
"name" => "nexus-6p"
]
15 => array:1 [
"name" => "nest"
]
16 => array:1 [
"name" => "moto-x"
]
17 => array:1 [
"name" => "macbook-pro"
]
18 => array:1 [
"name" => "mac-pro"
]
19 => array:1 [
"name" => "mac-book"
]
20 => array:1 [
"name" => "mac-book-air"
]
21 => array:1 [
"name" => "lg-g4"
]
22 => array:1 [
"name" => "laptop"
]
23 => array:1 [
"name" => "iphone-6"
]
24 => array:1 [
"name" => "iphone-6-s"
]
25 => array:1 [
"name" => "iphone-6-s-plus"
]
26 => array:1 [
"name" => "iphone-6-plus"
]
27 => array:1 [
"name" => "iphone-5s"
]
28 => array:1 [
"name" => "iphone-5"
]
29 => array:1 [
"name" => "iphone-4"
]
30 => array:1 [
"name" => "ipad"
]
31 => array:1 [
"name" => "ipad-pro"
]
32 => array:1 [
"name" => "hue"
]
33 => array:1 [
"name" => "htc-one-m9"
]
34 => array:1 [
"name" => "hp-laptop"
]
35 => array:1 [
"name" => "galaxy-s6"
]
36 => array:1 [
"name" => "galaxy-s6-edge"
]
37 => array:1 [
"name" => "galaxy-s5"
]
38 => array:1 [
"name" => "galaxy-note-5"
]
39 => array:1 [
"name" => "galaxy-note-4"
]
40 => array:1 [
"name" => "galaxy-note-3"
]
41 => array:1 [
"name" => "galaxy-note-2"
]
42 => array:1 [
"name" => "chromecast"
]
43 => array:1 [
"name" => "chrome-book"
]
44 => array:1 [
"name" => "blackberry-z30"
]
45 => array:1 [
"name" => "blackberry-z10"
]
46 => array:1 [
"name" => "blackberry-prev"
]
47 => array:1 [
"name" => "blackberry-bold"
]
48 => array:1 [
"name" => "belkin-wemo"
]
49 => array:1 [
"name" => "august"
]
50 => array:1 [
"name" => "apple-watch"
]
51 => array:1 [
"name" => "apple-tv"
]
52 => array:1 [
"name" => "androidtablet"
]
53 => array:1 [
"name" => "amazon-tv"
]
]
循环遍历我的数组,并检查我的$input
子字符串是否包含我的$devices
数组的设备名称之一。
foreach ($devices as $i=>$device) {
if( stripos($input, $device['name']) !== false){
dd('True');
}
else
dd('False');
}
False
打印。尽管如此,我知道one-plus-2
实际上存在于我的设备阵列中。
11 => array:1 ["name" => "one-plus-2"]
我错过了什么吗?
对此的任何提示/建议将不胜感激!
答案 0 :(得分:1)
您可以将array_filter()
与strpos()
:
$matches = array_filter($devices, function($device) use ($input) {
return strpos($input, $device['name']) !== FALSE;
});
dd($matches);
array_filter()
没有重新编制索引,因此第一个键不是0
,而是11
(在这种情况下)。
答案 1 :(得分:1)
stripos>>> strpos
“stripos”错误,正确的是“strpos”
且参数必须为
strpos(要搜索的字符串,您想要的字符串)
if (strpos($device['name'], $input) !== false) {
echo 'true';
}
答案 2 :(得分:0)
您已切换针和干草堆参数
试试这个
if( stripos($device['name'], $input) !== false){ ...}