1
votes

I am getting the following error while comparing two array using PHP.

Error:

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

I am explaining my code below.

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

Please help me to resolve this error.

1
What you want to check from the condition.... - PHP Geek
@PHPGeek : Here i need to check if $childata value is present inside $maindata array then the two echo will execute and that particular value will remove.Once $childata length will be zero then the 2nd else part will excute. - user5443928
Then I think you need a single dimensional array instead of 2-D array.. Is there any need of this array... - PHP Geek
@PHPGeek : Can you make this correct ? - user5443928
why don't you use array_udiff_assoc with a callback or something? - Taha Paksu

1 Answers

0
votes

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;
        }
    }