2016-11-15 99 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

相关问题