Problems comparing two variables in php

0

I have two sql server queries in php and I want to compare them.

I have them as follows:

  • $variable1 has N amount of data.
  • $variable2 has N amount of data.

Both variables save SQL Server queries from different tables but have the student number in common, what $variable1 contains the total number of students and $variable2 contains only some ... I want it show the ones that are the same in both variables ... I have the following code but it does not work

if($variable1 == $variable2){
$con++;
$variable1;
$variable2;
}else {
     if(($variable1 == $variable2) == null){
     $con++;
     $variable1;
     $variable2;
}
}

variable1 contains the following

1
2
3
4
5
6
7
8
9
10

and variable 2 contains the following

2
4
7
9

What I want to get is the following

1
2         2
3
4         4
5
6
7         7
8
9         9
10
    
asked by Pedro185 17.11.2018 в 17:42
source

1 answer

0

I understand that what you want to do is to check if the elements of an array are in another array (that the data comes from a database is the least).

The following code I think does what you want.

#!/usr/bin/php -q
<?php

$variable1 = array(2,3,4,5,6,7,8,9,10);
$variable2 = array(2,4,7,9);

foreach($variable1 as $comp)
{
    if (in_array($comp,$variable2))
        echo "$comp $comp\n";
    else
        echo "$comp\n";
}

Take a look at the in_array function.

    
answered by 17.11.2018 в 19:43