-1

Here's the two arrays dump

1st array

Array
(
    [0] => Array
        (
            [table_no] => T1
            [min_table] => 2
            [seat_per_table] => 2
        )

    [1] => Array
        (
            [table_no] => T2
            [min_table] => 2
            [seat_per_table] => 4
        )

    [2] => Array
        (
            [table_no] => T3
            [min_table] => 2
            [seat_per_table] => 4
        )

    [3] => Array
        (
            [table_no] => T4
            [min_table] => 2
            [seat_per_table] => 4
        )

    [4] => Array
        (
            [table_no] => T5
            [min_table] => 2
            [seat_per_table] => 4
        )

)

2nd array

Array
(
    [0] => Array
        (
            [table_no] => T3
        )

    [1] => Array
        (
            [table_no] => T4
        )

    [2] => Array
        (
            [table_no] => T5
        )

)

Expected Array

Array
(
        [0] => Array
            (
                [table_no] => T1
                [min_table] => 2
                [seat_per_table] => 2
            )

        [1] => Array
            (
                [table_no] => T2
                [min_table] => 2
                [seat_per_table] => 4
            )

)

I tried with array_diff(), but it doesn't give result.

Then tried with unset(),

foreach($example2 as $key => $value) {
   foreach($example1 as $key1 => $value1) {
      if ($value1 == $value) {
         unset($example2[$key])
      }
   }
}

this doesn't work.

How can i make this work properly?

Here i want to compare 'table_no' from array-1 and 'table_no' from array-2

Thanks.

user123456789
  • 556
  • 3
  • 12
  • 31

3 Answers3

1

I got it

foreach ($rest_tables as  $rtvalue){
    $res_rtable[]=$rtvalue->table_no;
}
foreach ($checklist as  $chvalue){
    $res_checklist[]=$chvalue->table_no;
}

$removeduplicate=array_intersect($res_rtable,$res_checklist);
$array_res = array_diff($res_rtable,$removeduplicate);

foreach($array_res as $key => $value){
    echo $value.'-'.$rest_tables[$key]->min_table.','.$rest_tables[$key]->seat_per_table.'<pre>';
}
user123456789
  • 556
  • 3
  • 12
  • 31
0

Maybe this answer will help you

$result = array_diff($first_array, $second_array);

print_r($result);
Dirk
  • 10,668
  • 2
  • 35
  • 49
dev4092
  • 2,820
  • 1
  • 16
  • 15
0

I'm guessing you're comparing by table_no in the first and restaurant_table in the second array?

Use array_udiff. It lets you use a callback to determine the difference between values. So for objects, where you don't necessarily want to just do $someobj == $anotherobj, it works well.

<?php

$t = new stdClass;
$t->table_no = 'T4';
$t2 = new stdClass;
$t2->table_no = 'T6';
$one = array(
    $t,
    $t2,
);

$t = new stdClass;
$t->restaurant_table = 'T4';
$two = array(
    $t,
);

$res = array_udiff($one, $two, function ($first, $second) {
    $no1 = isset($first->table_no) ? $first->table_no : (
        isset($first->restaurant_table) ? $first->restaurant_table : null
    );
    $no2 = isset($second->table_no) ? $second->table_no : (
        isset($second->restaurant_table) ? $second->restaurant_table : null
    );

    return strcmp($no1, $no2);
});

var_dump($res);

You're going to have a lot easier time doing this with objects that have the same property name for comparison.

chrisguitarguy
  • 2,329
  • 20
  • 18