Remove elements from arrays that are not in 2 PHP arrays

0
Array A

[0] => stdClass Object
        (
            [id] => 1542
            [id_restaurant] => 303
            [name] => Café da Manhã
            [hour] => 10:15:00
            [quantity] => 10
            [peoples] => 6
            [weekday] => 1
            [is_available] => 1
            [created_at] => 2018-09-21 16:28:23
            [updated_at] => 2018-09-21 16:28:23
        )

    [1] => stdClass Object
        (
            [id] => 1543
            [id_restaurant] => 303
            [name] => Almoço
            [hour] => 16:00:00
            [quantity] => 10
            [peoples] => 6
            [weekday] => 1
            [is_available] => 1
            [created_at] => 2018-09-21 16:28:23
            [updated_at] => 2018-09-21 16:28:23
        )

    [2] => stdClass Object
        (
            [id] => 1544
            [id_restaurant] => 303
            [name] => Janta
            [hour] => 17:45:00
            [quantity] => 10
            [peoples] => 6
            [weekday] => 1
            [is_available] => 1
            [created_at] => 2018-09-21 16:28:23
            [updated_at] => 2018-09-21 16:28:23
        )

    [3] => stdClass Object
        (
            [id] => 1545
            [id_restaurant] => 303
            [name] => Café da Manhã
            [hour] => 10:45:00
            [quantity] => 20
            [peoples] => 6
            [weekday] => 2
            [is_available] => 1
            [created_at] => 2018-09-21 16:28:23
            [updated_at] => 2018-09-21 16:28:23
        )

Array B

[0] => stdClass Object
        (
            [id] => 1540
            [id_restaurant] => 303
            [name] => Café da Manhã
            [hour] => 10:15:00
            [quantity] => 10
            [peoples] => 6
            [weekday] => 1
            [is_available] => 1
            [created_at] => 2018-09-21 16:28:23
            [updated_at] => 2018-09-21 16:28:23
        )

    [1] => stdClass Object
        (
            [id] => 1599
            [id_restaurant] => 303
            [name] => Almoço
            [hour] => 16:00:00
            [quantity] => 10
            [peoples] => 6
            [weekday] => 1
            [is_available] => 1
            [created_at] => 2018-09-21 16:28:23
            [updated_at] => 2018-09-21 16:28:23
        )

    [2] => stdClass Object
        (
            [id] => 1544
            [id_restaurant] => 303
            [name] => Janta
            [hour] => 17:45:00
            [quantity] => 10
            [peoples] => 6
            [weekday] => 1
            [is_available] => 1
            [created_at] => 2018-09-21 16:28:23
            [updated_at] => 2018-09-21 16:28:23
        )

    [3] => stdClass Object
        (
            [id] => 1545
            [id_restaurant] => 303
            [name] => Café da Manhã
            [hour] => 10:45:00
            [quantity] => 20
            [peoples] => 6
            [weekday] => 2
            [is_available] => 1
            [created_at] => 2018-09-21 16:28:23
            [updated_at] => 2018-09-21 16:28:23
        )

I have 2 arrays A and B ... I need to remove from both arrays elements whose ids are not in both arrays A and B eg: ids 1544 and 1545 exist in both arrays, which do not exist remove from both arrays.

    
asked by anonymous 27.09.2018 / 17:30

2 answers

3

Object array comparison

$array1 = array(
    (object) array("id" => 1),
    (object) array("id" => 3),
    (object) array("id" => 5),
    (object) array("id" => 6),
    (object) array("id" => 7)
);

$array2 = array(
    (object) array("id" => 1),
    (object) array("id" => 3),
    (object) array("id" => 5),
    (object) array("id" => 6),

);

$array3 = array();
foreach($array2 as $t) {
    $array3[] = $t->id;
}
$result = array_filter($array1, function($v) use($array3){
    return in_array($v->id, $array3);
});
print_r($result);

See it working

    
27.09.2018 / 18:04
0

If your vectors are not objects you can use the array_intersect that calculates the intersection between arrays. Returns an array containing all the array1 values that are present in the other arguments. Note that keys are preserved .
You can see here working.

    <?php
       $array1 = array("a" => "verde", "vermelho", "azul");
       $array2 = array("b" => "verde", "amarelo", "vermelho");
       $result = array_intersect($array1, $array2);
       print_r($result);
    ?>

If you have an object vector, you can use the function below:
You can see it working here

<?php

  $a = array(
      (object) array("id" => 1),
      (object) array("id" => 3),
      (object) array("id" => 5),
      (object) array("id" => 6),
      (object) array("id" => 7)
  );

  $b = array(
      (object) array("id" => 1),
      (object) array("id" => 3),
      (object) array("id" => 5),
      (object) array("id" => 6),

  );

  $result = array_map('unserialize',
      array_intersect(
          array_map('serialize',$a), 
          array_map('serialize',$b)
      )
  );

  print_r($result);

?>
    
27.09.2018 / 18:06