Compare values of 2 arrays with PHP

Asked

Viewed 486 times

2

Dear colleagues,

I have the following situation:

BD 01

ID | DESCRICAO | DRADS
1  | REGISTRO1 | 1,2,3
2  | REGISTRO2 | 1

BD 02

ID | DESCRICAO | DRADS
1  | REGISTRO1 | 1,2
2  | REGISTRO2 | 1

And I need to make a <select></select> pulling only BD 01 records that have the same DRADS as BD 02... That is, BD 01 will only be able to select the record(s) that is(s) with the same(s) DRADS(s of BD 02.

The values of BD 01, I get through a while(...). Already the values of BD 02, I get through $_SESSION['BD_02_DRADS'].

I have tried to compare the values of $_SESSION['BD_02_DRADS'] + $row['BD_01_DRADS'] with the functions in_array() and array_diff() but I couldn’t get the result I need.

Using in_array(), results that has more than one DRADS appear duplicated.

Using array_diff(), I can only display the <select></select> where DRADS are identical.

I do not know if I could be clear enough, but I will try to leave an example of what I need below:

while($row = $stmt->fetch(PDO::FETCH_ASSOC)){
     $BD_01_DRADS = explode(",", $row['BD_01_DRADS']);
     $BD_02_DRADS = explode(",", $_SESSION['BD_02_DRADS']);

     $array1 = $BD_01_DRADS;
     $array2 = $BD_02_DRADS;
}

So I need the following condition: SE TIVER $BD_01_DRADS EM $BD_02_DRADS, EXIBE UM <option>...</option> dentro do <select></select> com os respectivos dados deste registro.

Everyone, I apologize if I was not clear enough. But I am available for any clarification.

Any and all help will be welcome.

Thank you all!

1 answer

4


Does the use of array_intersect array resolve?

Code

<?php
 $array1 = array(1,2,3);
 $array2 = array(1,2);
 $result = array_intersect ($array1, $array2);
?>

Upshot

   array (
     0 => 1,
     1 => 2,
   )

I’m going to leave here a very useful statement about testing online arrays.

array_intersect

Available functions

  • Buddy, thanks for the feedback! Unfortunately due to the routine problems, I did not get time to move forward on the project. I will be testing the use of array_intersect and put the results in sequence.

  • I applied the array_intersect to the project and it worked perfectly for what I needed! Thank you for the strength!

Browser other questions tagged

You are not signed in. Login or sign up in order to post.