使用PHP比較兩個數組時遇到以下錯誤。使用PHP嘗試匹配兩個數組時遇到錯誤
錯誤:
Notice: Undefined offset: 1 in /opt/lampp/htdocs/test/search.php on line 7
Notice: Undefined offset: 1 in /opt/lampp/htdocs/test/search.php on line 15
delete the valueNotice: Undefined offset: 2 in /opt/lampp/htdocs/test/search.php on line 7
Notice: Undefined offset: 2 in /opt/lampp/htdocs/test/search.php on line 15
delete the value
我解釋下面我的代碼。
$maindata=array(array('id'=>3),array('id'=>7),array('id'=>9));
$childata=array(array('id'=>3),array('id'=>45));
for($i=0;$i<count($maindata);$i++){
//print_r($childata);
if(count($childata) > 0){
if(in_array($childata[$i],$maindata)){
echo "get the value \n".$maindata[$i]['id'];
echo "insert the value \n".$maindata[$i]['id'];
unset($childata[$i]);
if(count($childata) > 0){
$childata=array_values($childata);
}
}else{
echo "delete the value \n".$childata[$i]['id'];
unset($childata[$i]);
if(count($childata) > 0){
$childata=array_values($childata);
}
}
}else{
echo "get the value \n".$maindata[$i]['id'];
echo "insert the value \n".$maindata[$i]['id'];
}
}
請幫我解決這個錯誤。
要從條件檢查內容.... –
@PHPGeek:在這裏,我需要檢查是否'$ childata'值存在裏面'$ maindata'數組,那麼這兩個'echo'將執行並且該特定值將被刪除。一旦'$ childata'長度將爲零,則第二個else部分將執行。 – subhra
然後,我認爲你需要一個單維數組而不是二維數組..有沒有這個數組的需要... –