数组合并&数组唯一

发布于 2024-10-11 08:00:00 字数 454 浏览 4 评论 0原文

PHP 中是否有一个数组函数可以以某种方式执行 array_merge,比较,忽略键?我认为 array_unique(array_merge($a, $b)) 有效,但是我相信一定有更好的方法来做到这一点。

例如。

$a = array(0 => 0, 1 => 1, 2 => 2);
$b = array(0 => 2, 1 => 3, 2 => 4);

结果:

$ab = array(0 => 0, 1 => 1, 2 => 2, 3 => 3, 4 => 4);

请注意,我不关心 $ab 中的键,但是如果它们是升序的,从 0 开始到 count,那就太好了 ($ab)-1

Is there an array function in PHP that somehow does array_merge, comparing the values, ignoring the keys? I think that array_unique(array_merge($a, $b)) works, however I believe there must be a nicer way to do this.

eg.

$a = array(0 => 0, 1 => 1, 2 => 2);
$b = array(0 => 2, 1 => 3, 2 => 4);

resulting in:

$ab = array(0 => 0, 1 => 1, 2 => 2, 3 => 3, 4 => 4);

Please note that I don't care about the keys in $ab, however it would be nice if they were ascending, starting at 0 to count($ab)-1.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(5

城歌 2024-10-18 08:00:00

最优雅、最简单、最有效的解决方案是原始问题中提到的解决方案...

$ab = array_unique(array_merge($a, $b));

这个答案之前也在 Ben Lee 和 doublejosh 的评论中提到过,但我将其发布在这里作为实际答案,以造福其他人发现此问题并想知道最佳解决方案是什么的人,无需阅读此页面上的所有评论。

The most elegant, simple, and efficient solution is the one mentioned in the original question...

$ab = array_unique(array_merge($a, $b));

This answer was also previously mentioned in comments by Ben Lee and doublejosh, but I'm posting it here as an actual answer for the benefit of other people who find this question and want to know what the best solution is without reading all the comments on this page.

原来分手还会想你 2024-10-18 08:00:00

为了回答所提出的问题,对于在保留键的同时也可以使用关联数组的通用解决方案,我相信您会发现这个解决方案最令人满意:

/**
 * array_merge_unique - return an array of unique values,
 * composed of merging one or more argument array(s).
 *
 * As with array_merge, later keys overwrite earlier keys.
 * Unlike array_merge, however, this rule applies equally to
 * numeric keys, but does not necessarily preserve the original
 * numeric keys.
 */
function array_merge_unique(array $array1 /* [, array $...] */) {
  $result = array_flip(array_flip($array1));
  foreach (array_slice(func_get_args(),1) as $arg) { 
    $result = 
      array_flip(
        array_flip(
          array_merge($result,$arg)));
  } 
  return $result;
}

To answer the question as asked, for a general solution that also works with associative arrays while preserving keys, I believe that you will find this solution most satisfactory:

/**
 * array_merge_unique - return an array of unique values,
 * composed of merging one or more argument array(s).
 *
 * As with array_merge, later keys overwrite earlier keys.
 * Unlike array_merge, however, this rule applies equally to
 * numeric keys, but does not necessarily preserve the original
 * numeric keys.
 */
function array_merge_unique(array $array1 /* [, array $...] */) {
  $result = array_flip(array_flip($array1));
  foreach (array_slice(func_get_args(),1) as $arg) { 
    $result = 
      array_flip(
        array_flip(
          array_merge($result,$arg)));
  } 
  return $result;
}
_蜘蛛 2024-10-18 08:00:00
function umerge($arrays){
 $result = array();
 foreach($arrays as $array){
  $array = (array) $array;
  foreach($array as $value){
   if(array_search($value,$result)===false)$result[]=$value;
  }
 }
 return $result;
}
function umerge($arrays){
 $result = array();
 foreach($arrays as $array){
  $array = (array) $array;
  foreach($array as $value){
   if(array_search($value,$result)===false)$result[]=$value;
  }
 }
 return $result;
}
︶ ̄淡然 2024-10-18 08:00:00

array_merge 将忽略数字键,因此在您的示例中 array_merge($a, $b) 将为您提供 $ab,无需调用 array_unique()

如果您有字符串键(即关联数组),请首先使用 array_values() :

array_merge(array_values($a), array_values($b));

array_merge will ignore numeric keys, so in your example array_merge($a, $b) would give you $ab, there is no need to call array_unique().

if you have string keys (i.e. an associative array) use array_values() first:

array_merge(array_values($a), array_values($b));
匿名。 2024-10-18 08:00:00
$a = array(0 => 0, 1 => 1, 2 => 2);
$b = array(0 => 2, 1 => 3, 2 => 4);

//add any from b to a that do not exist in a
foreach($b as $item){


    if(!in_array($item,$b)){
        $a[] = $item
    }

}

//sort the array
sort($a);
$a = array(0 => 0, 1 => 1, 2 => 2);
$b = array(0 => 2, 1 => 3, 2 => 4);

//add any from b to a that do not exist in a
foreach($b as $item){


    if(!in_array($item,$b)){
        $a[] = $item
    }

}

//sort the array
sort($a);
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文