array_merge &array_unique [英] array_merge & array_unique

查看:22
本文介绍了array_merge &array_unique的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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

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.

例如

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

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.

推荐答案

最优雅、最简单、最高效的解决方案就是原问题中提到的那个...

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

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

此答案之前也在 Ben Lee 和 doublejosh 的评论中提到过,但我将其发布在这里作为实际答案,以供其他发现此问题并想在不阅读所有内容的情况下了解最佳解决方案的人的利益此页面上的评论.

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.

这篇关于array_merge &array_unique的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆