array_merge&安培; array_unique [英] array_merge & array_unique
本文介绍了array_merge&安培; array_unique的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
有没有在PHP的数组功能,不知怎的不array_merge,比较值,忽视了钥匙?我认为, array_unique(array_merge($ A,$ B))
的作品,但是我相信一定有这样做一个更好的方式。
如:
$一个=阵列(0 =大于0,1 =大于1,2 =大于2);
$ B =阵列(0 =→2,1 =→3,2 =→4);
造成:
$ AB =阵列(0 =大于0,1 =大于1,2 =大于2,3 =→3,4 =→4);
请注意,我不关心键 $ AB
,但它的将是不错的,如果他们被上升的开始, 0 计数($ AB)-1
。
解决方案
函数umerge($数组){
$结果=阵列();
的foreach($数组作为$数组){
$阵列=(数组)$数组;
的foreach($数组作为$值){
如果(array_search(价值$,$结果)===假)$结果[] = $价值;
}
}
返回$结果;
}
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
.
解决方案
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;
}
这篇关于array_merge&安培; array_unique的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文