2016-02-26 74 views
0

我有

一個$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"] 

我錯過了什麼嗎?

任何提示/建議,將非常感謝!

+1

我在想你在使用Laravel。 'dd()'退出,所以它會在第一項之後破壞外觀:'xperia-z-ultra',因爲它不匹配。 – Rudie

+0

@Rudie:哦,哇。我沒有那個。測試這個的正確方法是什麼? ;) – ihue

+0

要清楚,你想檢查任何數組鍵是否包含** any ** common substring?因爲「約翰的一加二」!=「一加二」。通過這種邏輯,許多這些將通過,因爲其中許多包含字母「o」... –

回答

0

你交換針和乾草堆參數

if(stripos($device['name'], $input) !== false){ ...} 
+0

相同的結果 - 即使我交換後。我的'真'從不打印。 – ihue

+0

顯然它不會,因爲「johns一加二」是不一樣的「一加二」 –

+0

所以同樣不會考慮爲子串?請指教。 – ihue

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

stripos函數>>> strpos

「stripos函數」 錯正確的是 「strpos」

和參數必須

strpos(以搜索的字符串,你想要的字符串)

if (strpos($device['name'], $input) !== false) { 
    echo 'true'; 
} 
+0

'stripos'不區分大小寫'strpos',所以也應該工作。 – Rudie

+0

是的,對不起。我認爲你的代碼工作。我在這裏測試 http://codepad.org/D9a5XGUd。 – Akram

相關問題