這個(gè)問題已經(jīng)在這里有了答案:????????????>????????????PHP in_array() / array_search() odd behaviour????????????????????????????????????2個(gè) 我正在嘗試調(diào)試編寫的腳本,并且有一個(gè)問題歸結(jié)為檢查(多維)資產(chǎn)數(shù)組中是否存在標(biāo)識符.我正在使用一個(gè)in_array函數(shù),該函數(shù)以遞歸方式搜索從this question獲得的結(jié)果.
這是函數(shù):
function in_array_r($needle, $haystack, $strict = false) {
foreach ($haystack as $item) {
if (($strict ? $item === $needle : $item == $needle) || (is_array($item) && in_array_r($needle, $item, $strict))) {
return true;
}
}
return false;
}
我正在使用此數(shù)據(jù):
針頭:“ B51MM36” 干草堆:(針對未美化的數(shù)組的道歉-找不到從var_export美化的方法)
$sedols = array ( 0 => array ( 'ipsid' => '72', 'buyList' => '1', 'sedol' => 'B8LFDR7', 'isin' => 'LU0827876409', 'currency' => NULL, 'hedged' => '0', 'acc' => '0', 'inst' => '0', 'description' => 'BlackRock European Long Only', 'nonUKsitus' => '0', 'reportingStatus' => '0', 'matchScore' => 0, ), 1 => array ( 'ipsid' => '72', 'buyList' => '1', 'sedol' => 'LU0827876151', 'isin' => 'LU0827876151', 'currency' => 'USD', 'hedged' => '1', 'acc' => '1', 'inst' => '0', 'description' => 'Blackrock European Long Only', 'nonUKsitus' => '0', 'reportingStatus' => '0', 'matchScore' => 0, ), 2 => array ( 'ipsid' => '72', 'buyList' => '1', 'sedol' => 'LU0406496546 ', 'isin' => 'LU0406496546 ', 'currency' => 'EUR', 'hedged' => '1', 'acc' => '1', 'inst' => '0', 'description' => 'Blackrock European Long Only', 'nonUKsitus' => '0', 'reportingStatus' => '0', 'matchScore' => 0, ), 3 => array ( 'ipsid' => '72', 'buyList' => '1', 'sedol' => 'LU0827876409', 'isin' => 'LU0827876409', 'currency' => 'GBP', 'hedged' => '1', 'acc' => '0', 'inst' => '0', 'description' => 'Blackrock European Long Only', 'nonUKsitus' => '0', 'reportingStatus' => '1', 'matchScore' => 1, ), );
當(dāng)我運(yùn)行var_dump(in_array_r(‘B51MM36’,$sedols));它輸出bool(true).我很困惑,因?yàn)樽址?B51MM36”沒有出現(xiàn)在干草堆數(shù)組中的任何位置.任何人都可以確定這里發(fā)生了什么嗎? 解決方法: 原因是
var_dump('B51MM36' == 0);
是真的,不知道為什么(也許將字符串轉(zhuǎn)換為整數(shù)),但是這項(xiàng)工作
var_dump(in_array_r('B51MM36', $sedols, true));
嘗試刪除嚴(yán)格的選項(xiàng) 來源:https://www./content-1-536601.html
|