user3801544
user3801544

Reputation: 31

How can I compare two arrays of data retrieve from database?

I have two arrays. In one array, I store data from one table. I store data from another table in the other array. I wanted to compare data from both of the arrays. If the data of the first array is in the second array, I want to proceed. How can I perform this?

I tried the following code but it's not working event though the array1 number exists in array2:

$x = "SELECT * FROM table1";
$data1 = mysqli_query($link, $x);
$dat1 = array()
while($row1= mysqli_fetch_array($data,MYSQLI_ASSOC))
{
  $dat1[] = $row1;
  $f1 = $row1['fid'];
}

$y = "SELECT * FROM table2";
$data2 = mysqli_query($link, $y);
$dat2 = array()
while($row2= mysqli_fetch_array($data2,MYSQLI_ASSOC))
{
  $dat2[] = $row2;
  $f2 = $row2['fid'];
}

if(in_array($dat1,$dat2))
{
  // if exists proceed
}
else
{
  // if not show error
}

Upvotes: 3

Views: 1342

Answers (7)

user3121354
user3121354

Reputation: 1

According to: SQL for Dummies, the INNER JOIN discards all rows from the result table that DO NOT have corresponding rows in both source tables.

Try:

SELECT column_name(s)
FROM table1
INNER JOIN table2
ON table1.column_name=table2.column_name;

Upvotes: 0

Eagle1
Eagle1

Reputation: 810

putting aside the sql part, use this function:

function compare_array($arr1,$arr2)
{
    foreach($element as $arr1)
    {    
        foreach($element2 as $arr2)
        {
            if($element2 == $element)
            {
               return true;
            }
            else
            {

            }
        }
 return false;
}

pass it your two arrays obtained with your queries and act upon its return it might be a bit heavy but if I understand well what you need it should work

Upvotes: 0

Arron
Arron

Reputation: 916

As sotirojo just pointed out, don't try to do data stuff in PHP when you can do it in a database!
So any answer that tells you how to solve your issue within PHP in my eyes will be a wrong answer.

SELECT table_1.* 
   FROM table_1
   JOIN table_2 USING(fid)

Will return you all rows that exist in both table_1 as table_2

SELECT table_1.* 
   FROM table_1
   LEFT JOIN table_2 USING(fid)
WHERE table_2.fid IS NULL

Will return you all rows in table_1 that are not in table_2

And so on.

Use your database for doing data things.

Upvotes: 0

dxritchie
dxritchie

Reputation: 138

According to your last comment your logic is wrong in your code then. You are currently pulling all rows into your $dat1 variable from your mysql return so you would not be able to check if a specific value exists in the 2nd array by using the entire array as a single value. You are also overwriting $f1 on each iteration of your while loop.

You could check this for each ID returned from your first array like this.

$x = "SELECT * FROM table1";
$data1 = mysqli_query($link, $x);
$dat1 = array()
while($row1= mysqli_fetch_array($data,MYSQLI_ASSOC))
{
  $dat1[] = $row1;
  $f1 = $row1['fid'];
  $y = "SELECT * FROM table2 where fid = ".$f1;
  $data2 = mysqli_query($link, $y);
  $dat2 = array();
  while($row2= mysqli_fetch_array($data2,MYSQLI_ASSOC))
  {
    //data exists in table2
  }
}

Upvotes: 0

sotirojo
sotirojo

Reputation: 11

This can be done by SQL.

To check if all fids in table1 are in table2:

SELECT COUNT(a.fid) FROM table1 AS a WHERE a.fid IN (SELECT b.fid FROM table2 AS b)

SELECT COUNT(*) FROM table1

If the two values are equal, then all fids in table1 are in table2.

Upvotes: 1

ALOK
ALOK

Reputation: 553

You can use this way

 $bool=0;
 foreach ($dat1 as $a) {
    foreach ($dat2 as $b) {
        if($a==$b)
        {
            $bool=1;
            break;
        }
        else
        {
            $bool=0;
        }
    }
    if($bool==1)
    {
        break;
    }
 }
 if($bool==1)
 {
   //proceed
  }
 else
  {
     //error
   }

Here if anyone of the value in dat2 is present in dat1 then it will be true and you can proceed.

Upvotes: 0

Eagle1
Eagle1

Reputation: 810

if(in_array($data,$dat2))
{
  //if exists proceed
}
else
{
  //if not show error
}

why do you put $data here ? should be $dat1 no ?

Upvotes: 0

Related Questions