【问题标题】:how to merge two arrays without duplication using a certain value in PHP?如何使用 PHP 中的某个值合并两个数组而不重复?
【发布时间】:2018-12-31 09:40:09
【问题描述】:

在PHP中,我有这两个数组,第一个是“$ar1”,第二个是“$ar2”:

Array
(
    [0] => stdClass Object
        (
            [id] => 1505
            [category] => blue
        )

    [1] => stdClass Object
        (
            [id] => 1805
            [category] => red
        )

)
Array
(
    [1777] => stdClass Object
        (
            [id] => 1505
            [category] => yellow
        )

    [1877] => stdClass Object
        (
            [id] => 1507
            [category] => blue
        )

)

我想将 $ar1 添加到 $ar2,但前提是“id”不在 $ar2 中。 例如:不应将 ID 为“1505”的那个添加到 $ar2。 这是我使用的代码:

$ar3 = array_merge_recursive($ar1, $ar2);

【问题讨论】:

标签: php arrays


【解决方案1】:

一种可能是首先使用array_column$ar2 中提取ID,然后使用array_filter

$ar2 = array( '1777' => (object) array('id' => '1505', 'category' => 'yellow'), '1877' => (object) array('id' => '1507', 'category' => 'blue'), );
$ids = array_column($ar1, 'id');
$ar3 = array_merge_recursive($ar1, array_filter($ar2, function($x) use ($ids) {
    return !in_array($x->id, $ids);
}));

print_r($ar3);

结果:

Array
(
    [0] => stdClass Object
        (
            [id] => 1505
            [category] => blue
        )

    [1] => stdClass Object
        (
            [id] => 1805
            [category] => red
        )

    [2] => stdClass Object
        (
            [id] => 1507
            [category] => blue
        )

)

Php demo

【讨论】:

  • 你有两次 id 1805 - 为什么?
  • @DavidWinder 我已经更新了,我切换了过滤。
  • array_merge 不会在这里做吗?
【解决方案2】:

可能不是最 php-ish 的代码(我想有一些数组函数可以做得更好),但它正在工作:

$array1 = array(
    new Dummy(1505,"blue"),
    new Dummy(1805,"red")
);

$array2 = array(
    new Dummy(1505,"yellow"),
    new Dummy(1507,"blue")
);

$array3 = [];

foreach($array1 as $value)
{
    $array3[$value->id] = $value;
}

foreach($array2 as $value)
{
    if(!isset($array3[$value->id]))
    {
        $array3[$value->id] = $value;
    }
}

var_dump($array3);

我刚刚做了一个 Dummy 类,因为我想尽可能地接近你的数据。这使用映射的键(数组是 PHP 中的映射)作为主键。首先,您遍历始终设置的数组,然后遍历仅在未设置 id 时才应用于最终数组的数组 -> 完成。这打印:

array(3) { 
[1505]=> object(Dummy)#1 (2) { ["id"]=> int(1505) ["category"]=> string(4) "blue"}
[1805]=> object(Dummy)#2 (2) { ["id"]=> int(1805) ["category"]=> string(3) "red" } 
[1507]=> object(Dummy)#4 (2) { ["id"]=> int(1507) ["category"]=> string(4) "blue" } 
}

【讨论】:

    【解决方案3】:

    试试这个来合并数组

    array_merge($array1,$array2)
    

    【讨论】:

    • 您可以合并,但这不会过滤掉已经在第一个数组中的 id。 3v4l.org/U46vj
    【解决方案4】:

    试试这个,功能来自这个链接:http://php.net/manual/en/function.array-unique.php

    $array1 = array(
                        array('id'=>'1505', 'category'=>'blue'),
                        array('id'=>'1805', 'category'=>'red')
                    );
    
          $array2 = array(
                        array('id'=>'1505', 'category'=>'yellow'),
                        array('id'=>'1507', 'category'=>'blue')
                    );
          $array3 = array_merge($array1, $array2);
    
          function unique_multidim_array($array, $key) { 
              $temp_array = array(); 
              $i = 0; 
              $key_array = array(); 
    
              foreach($array as $val) { 
                  if (!in_array($val[$key], $key_array)) { 
                      $key_array[$i] = $val[$key]; 
                      $temp_array[$i] = $val; 
                  } 
                  $i++; 
              } 
              return $temp_array; 
          } 
          $details = unique_multidim_array($array3,'id');
          echo "<pre>";
          print_r($details);
          die;
    

    【讨论】:

      猜你喜欢
      • 2021-09-11
      • 2019-02-22
      • 2021-11-28
      • 1970-01-01
      • 2021-07-01
      • 2013-12-27
      • 2013-09-12
      • 1970-01-01
      • 2016-03-13
      相关资源
      最近更新 更多