[英]Search substring in a PHP Array
我試圖在如下所示的PHP數組中找到特定的字符串。
Array ( [0] => Array ( [0] => sku,qty,is_in_stock ) [1] => Array ( [0] => EBCDAA014MAS34B,149.000000,1 ));
我正在如下准備上面的數組,並將其存儲在名為$listing
的變量中
我正在使用array_find()
函數來做到這一點。
function array_find($needle, array $haystack)
{
foreach ($haystack as $key => $value) {
if (false !== stripos($value, $needle)) {
return $key;
}
}
return false;
}
$hello = array_find('EBCDAA014MAS34B', $listing);
echo $hello;
但仍然,我得到的回報是虛假的。 有人可以告訴我哪種方法是正確的嗎?
您的數組是一個數組數組,其中字符串是每個數組中的第一項,其$value[0]
嘗試更改此行:
if (false !== stripos($value, $needle)) {
到這行:
if (false !== stripos($value[0], $needle)) {
只需使用in_array() PHP函數,請參見以下示例。
$listing = Array ( [0] => Array ( [0] => sku,qty,is_in_stock ) [1] => Array ( [0] => EBCDAA014MAS34B,149.000000,1 ));
in_array('EBCDAA014MAS34B', $listing)
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.