【问题标题】:Two arrays difference in phpphp中两个数组的区别
【发布时间】:2014-02-10 04:58:53
【问题描述】:

这是两个数组转储

第一个数组

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
        )

)

第二个数组

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

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

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

)

预期数组

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
            )

)

我尝试了array_diff(),但没有给出结果。

然后尝试使用unset()

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

这不起作用。

我怎样才能使它正常工作?

这里我想比较 array-1 中的 'table_no' 和 array-2 中的 'table_no'

谢谢。

【问题讨论】:

  • 这些是对象数组。您需要在array_diff()之前将它们转换为使用
  • 你的两个数组中的对象完全不同。
  • 看看这个线程。 [php中两个数组的区别][1][1]:stackoverflow.com/questions/8826908/…
  • array_diff() 只比较数组元素的字符串值。除非您的对象有 toString 方法,否则您将需要编写自己的函数。 us3.php.net/array_diff
  • 您期望结果是什么?你能贴一个例子吗?

标签: php arrays


【解决方案1】:

我明白了

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>';
}

【讨论】:

    【解决方案2】:

    也许这个答案会对你有所帮助

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

    【讨论】:

      【解决方案3】:

      我猜你是在比较第一个数组中的table_no 和第二个数组中的restaurant_table

      使用array_udiff。它允许您使用回调来确定值之间的差异。所以对于那些你不一定只想做$someobj == $anotherobj的对象,它工作得很好。

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

      使用具有相同属性名称的对象进行比较会更容易。

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 2014-02-26
        • 1970-01-01
        • 2016-08-30
        • 2021-12-15
        相关资源
        最近更新 更多