Remove array elements that are not in 2 PHP arrays

Asked

Viewed 96 times

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 the elements whose id are not in both arrays A and B ex: ids 1544 and 1545 exist in both arrays, the ones that do not exist need to be removed from both arrays.

  • Your question got confused, from what I understand, you want to remove from both array items that do not exist in both array. Assemble an example of the return you need and also put the code you’ve tried.

  • ready now ta working.... had not noticed that you wanted to separate the ones that exist in the 2

  • ae man validates the answer to anyone who needs to know which one was used

2 answers

3


Comparison of object array

$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 working

0

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

    <?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);

?>

Browser other questions tagged

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