subhra subhra - 25 days ago 7
PHP Question

Getting error while trying to match two array using PHP

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.

Answer

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