我有
一個串$input
=「約翰的一個加2」,和遍歷一個數組,並檢查值中的一個是一個字符串
一個陣列的一部分$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"]
我錯過了什麼嗎?
任何提示/建議,將非常感謝!
我在想你在使用Laravel。 'dd()'退出,所以它會在第一項之後破壞外觀:'xperia-z-ultra',因爲它不匹配。 – Rudie
@Rudie:哦,哇。我沒有那個。測試這個的正確方法是什麼? ;) – ihue
要清楚,你想檢查任何數組鍵是否包含** any ** common substring?因爲「約翰的一加二」!=「一加二」。通過這種邏輯,許多這些將通過,因爲其中許多包含字母「o」... –