php PHP按值合并数组
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/7973915/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
PHP merge arrays by value
提问by Osvaldo Mercado
I know this is quite easily accomplished with a foreach
, then a while
->list, etc procedure, (I have already accomplished it), however I sense that my code is a bit dirty and it doesn't look like the best solution... I'm looking to use native PHP array functions to do the following:
我知道这很容易用一个foreach
,然后一个while
-> 列表等过程来完成,(我已经完成了),但是我觉得我的代码有点脏,它看起来不是最好的解决方案......我希望使用本机 PHP 数组函数来执行以下操作:
I have two arrays that look like this:
我有两个看起来像这样的数组:
[0] (Array)#2 [rank] "579" [id] "1" [1] (Array)#4 [rank] "251" [id] "2" [0] (Array)#2 [size] "S" [status] "A" [id] "1" [1] (Array)#15 [size] "L" [status] "A" [id] "2"
And I need as a result something like the following:
因此,我需要如下内容:
[0] (Array)#2 [size] "S" [status] "A" [id] "1" [rank] "579" [1] (Array)#2 [size] "L" [status] "A" [id] "2" [rank] "251"
Is there a way to be able to merge two arrays with the id
value (or ay other) without going into a endless set of foreach
s?
有没有办法在id
不进入无穷无尽的foreach
s集合的情况下将两个数组与值(或其他)合并?
回答by Peter
$array = array_merge_recursive($array1, $array2);
or make your own function (it may be faster)
或制作自己的功能(可能会更快)
function my_array_merge(&$array1, &$array2) {
$result = Array();
foreach($array1 as $key => &$value) {
$result[$key] = array_merge($value, $array2[$key]);
}
return $result;
}
$array = my_array_merge($array1, array2);
print_r($array);
回答by Martijn
As Ray noticed in a comment, the accepted answer does not actually answer the question. I was unable to find an answer, so I created the following small utility function:
正如 Ray 在评论中注意到的那样,接受的答案实际上并没有回答这个问题。我找不到答案,所以我创建了以下小实用函数:
function array_merge_callback($array1, $array2, $predicate) {
$result = array();
foreach ($array1 as $item1) {
foreach ($array2 as $item2) {
if ($predicate($item1, $item2)) {
$result[] = array_merge($item1, $item2);
}
}
}
return $result;
}
Use it as follows:
使用方法如下:
array_merge_callback($array1, $array2, function ($item1, $item2) {
return $item1['id'] == $item2['id'];
});
回答by gzhegow
Have a nice one to merging arrays like another languages. It's because php have auto numbering array elements, and merging will dublicate or replace different elements by keys.
像另一种语言一样合并数组。这是因为 php 具有自动编号数组元素,合并会通过键复制或替换不同的元素。
Now, it's changed.
现在,它变了。
// array_fork
public static function array_fork() {
$args = func_get_args();
$result = array();
foreach ($args as $arr) {
is_array($arr) || exit('[' . __METHOD__ . '] Each item must be an array.');
foreach ($arr as $key => $val) {
if (is_array($val)) {
// recursion
!isset($result[$key]) && $result[$key] = array();
$result[$key] = self::array_fork($result[$key], $arr[$key]);
continue;
}
if (is_numeric($key)) {
if (!in_array($arr[$key], $result))
$result[] = $arr[$key];
} else
$result[$key] = $arr[$key];
}
}
return $result;
}
回答by nilesh
function custom_array_merge(&$array1, &$array2) {
$result = Array();
foreach ($array1 as $key_1 => &$value_1) {
// if($value['name'])
foreach ($array2 as $key_1 => $value_2) {
if($value_1['name'] == $value_2['name']) {
$result[] = array_merge($value_1,$value_2);
}
}
}
return $result;
}
// Pass $array1, &$array2 and change the $value_2['name'] // name based on which u want to merge.
// 传递 $array1, &$array2 并更改 $value_2['name'] // 基于您要合并的名称。
回答by bigblind
ok, let's suppost your arrays are called $arr1 and $arr2, you could do this:
好的,让我们假设你的数组被称为 $arr1 和 $arr2,你可以这样做:
<?php
$newarray = Array();
foreach ($arr1 as $element=>$value){
$newarray = array_merge($arr1[$element],$arr2[$element])
}
?>