2016-11-15 91 views
1

使用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 value

Notice: 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']; 
     } 
    } 

請幫我解決這個錯誤。

+0

要從條件檢查內容.... –

+0

@PHPGeek:在這裏,我需要檢查是否'$ childata'值存在裏面'$ maindata'數組,那麼這兩個'echo'將執行並且該特定值將被刪除。一旦'$ childata'長度將爲零,則第二個else部分將執行。 – subhra

+0

然後,我認爲你需要一個單維數組而不是二維數組..有沒有這個數組的需要... –

回答

1

It's somewhat unclear why you are doing exactly the same thing in both the if and else Clauses within Your Loop. However; below is a Snippet that might possibly give you a little idea on how to perhaps revisit your approach. Quick-Test Here :

<?php 

    $mainData = array(array('id'=>3), array('id'=>7), array('id'=>9)); 
    $childData = array(array('id'=>3), array('id'=>45)); 

    foreach($mainData as $iKey=>$subArray){ 
     echo "get the value \n"  . $mainData[$iKey]['id'] . "<br />"; 
     echo "insert the value \n" . $mainData[$iKey]['id'] . "<br />"; 
     // CHECK TO SEE THAT THE ARRAY $childData IS NOT EMPTY 
     if(!empty($childData)){ 
      // THEN LOOP THROUGH THE $childData ARRAY 
      // AND IF YOU SIMILAR ELEMENT IS FOUND TO EXIST IN $mainData 
      // DELETE (UNSET) ITS EQUIVALENT IN THE $childData 
      foreach($childData as $iKey2=>$subArray2){ 
       if(in_array($subArray2, $mainData)){ 
        unset($childData[$iKey2]); 
       }else{ 
        // YOU ARE DOING EXACTLY THE SAME THING YOU DID IN THE IF CLAUSE 
        // HERE AGAIN IN THE ELSE CLAUSE: IS THAT WHAT YOU REALLY WANT? 
        // THAT IS YOU ARE REPEATING: "unset($childData[$iKey2]);" ??? 
        echo "delete the value \n" . $childData[$iKey2]['id'] . "<br />"; 
        unset($childData[$iKey2]); 

        // MOST LIKELY; YOU DON'T NEED THIS ELSE CLAUSE AT ALL 
        // unset($childData[$iKey2]); 
       } 
      } 
      // GET THE ARRAY VALUES IF THE $childData is NOT EMPTY 
      $childData = (!empty($childData))? array_values($childData):$childData; 
     } 
    } 
+0

@Pioz:讓我來清除我的需求。對於兩個數組之間的任何常見值,兩個'echo'語句將執行,剩餘的值將執行單個'echo'語句。 – subhra

+0

@subhra再次檢查代碼.... https://eval.in/677729順便說一下,名稱是** Poiz **不***皮奧茲*** **; - )** – Poiz

+0

對不起第一。好吧,它不按預期工作。讓我再解釋一次。在第一個數組('$ mainData')的所有值中,必須執行兩個'echo'。如果在第二個數組中存在任何常見值('here 3'),將被忽略。除此之外,任何剩餘的值都在第二個數組('i,e- $ childData')中執行。 – subhra

相關問題