0
當我嘗試使用in_array功能在PHP第二次對相同的數組變量i得到以下錯誤說:變量不是數組PHP
in_array()預計參數2爲陣列,串給定的在
當我包裝在條件is_array的功能,則返回false,我已經打印使用的print_r及其表示陣列結構的變量,這裏的代碼:
$chosenCour = array();
$chosenServ = array();
foreach ($preferences as $preference) {
if(!in_array($preference['courier'],$chosenCour)){
$chosenCour[] = $preference['courier'];
}
if(!in_array($preference['courier_service'],$chosenServ)){
$chosenServ[]= $preference['courier_service'];
}
}
foreach ($couriers as $courier) {
$courCond = false;
if(is_array($chosenCour)){
if(in_array($courier['courier_id'],$chosenCour)){
$courCond = true;
}
}
}
**定義:**'in_array(混合$針,數組$草垛[,布爾$嚴格= FALSE])' - 這意味着,你定義你想搜索首先,然後設置所需的數組,而不是相反的方式,就像你做'in_array($ preference ['courier'],$ chosenCour)' – Peon
@DainisAbols我第一次使用in_array時沒有問題,但第二次,我想要重用的變量即使在is_array函數中也不會被識別爲數組 –