0

I have 2 SELECT statement in my PHP. Both the select statements fetch data from two different DB. The fetched data is saved in PDO Assoc Array. The problem is when I want to compare those two arrays to find that if the column 'id' exist in both arrays or not. If it exists then ignore it. If it's a unique id then save it into a third array. But I found some problems in my Logic Below

And after running the below code I am getting a couple of error: 1: Array to string conversion 2: duplicate key value violates unique constraint

$arr1 = $msql->fetchAll(PDO::FETCH_ASSOC);
$array1 = array();
foreach($arr1 as $x){
$array1[] = $x['id'];
}


$arr2 = $psql->fetechAll(PDO::FETCH_ASSOC);
$array2 = array();
foreach($arr2 as $y){
  $array2[] = $y['id'];
 }

$finalarray = array();
for ($i = 0; $i < count($arr1); $i++){
if(count(array_intersect($array1,$array2)) <= 1){// is the count of id is 1 or less save that whole row in the $finalarray
      $finalarray = $arr1[$i]; // saving the unique row.
}
else{
    continue;
}
}

All I am trying to get the unique row of data array() after comparing their id column.

6
  • You had asked similar question here , should have edited the same question . Please delete older post now Commented Mar 8, 2018 at 5:52
  • I did Thank you. Commented Mar 8, 2018 at 5:55
  • What line is the error being reported on? Commented Mar 8, 2018 at 5:57
  • The code runs fine after @Gufran Hasan fixed it. But the error comes when there is a duplicate already exist in the db while inserting the data which fails to execute the query completely. Commented Mar 8, 2018 at 6:01
  • Then you should either prevent duplicate entries in $array1 , $array2 while assigning or use array_unique to $finalarray Commented Mar 8, 2018 at 6:03

4 Answers 4

1

You can use in_array() function as both arrays are index array.

$finalarray = array();
for ($i = 0; $i < count($arr1); $i++){
if(count(array_intersect($array1,$array2)) <= 1){// is the count of id is 1 or less save that whole row in the $finalarray
      $finalarray = $arr1[$i]; // saving the unique row.
}
else{
    continue;
}
}

make change in code:

$finalarray = array();
for ($i = 0; $i < count($arr1); $i++){
if(!in_array($array1[$i], $array2)){
      $finalarray[] = $array1[$i]; // saving the unique row.
}

}
Sign up to request clarification or add additional context in comments.

8 Comments

should be : $finalarray[] = $array1[$i]; and also no need of continue
@Gufran Hasan I am getting a syntax error in if statement?
I have updated my amswer please check it again hope it will for wrok as you are looking
@GufranHasan I don't know why but still is throwing me an error of unique value exist and then stops inserting?
@GufranHasan ERROR: Unique violation: 7 ERROR: duplicate key value violates unique constraint
|
1

You can simply use array_intersect() to get common values between two array. for difference, can use array_diff()

$array1 = [1,2,3,4,5,6,7];
$array2 = [2,4,6];
//array_intersect — Computes the intersection of arrays
$result = array_intersect($array1, $array2);
print_r($result);
//array_diff — Computes the difference of arrays
$result = array_diff($array1, $array2);
print_r($result);

DEMO

Comments

1

Rather than using 3 different arrays to get unique ids, you can do it by using one array. Make changes to your code as below:

$finalarray = array();

$arr1 = $msql->fetchAll(PDO::FETCH_ASSOC);
foreach($arr1 as $x){
    if (!in_array($x['id'],$finalarray)) { // check id is already stored or not       
        $finalarray[] = $x['id'];
    }
}


$arr2 = $psql->fetechAll(PDO::FETCH_ASSOC);
foreach($arr2 as $y){  
    if (!in_array($y['id'],$finalarray)) { // check id is already stored or not       
        $finalarray[] = $y['id'];
    }
}

Comments

0

Maybe you should make sure which array is larger before your loop ;

Or using array_diff:

$finalarray = array_diff($array1 , $array2) ?? [];
$finalarray = array_merge( array_diff($array2 , $array1) ?? [], $finalarray );

1 Comment

My array1() will always be larger then array2()

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.