How to merge n number of array in php. I mean how can I do the job like :array_merge(from : $result[0], to : $result[count($result)-1])
ORarray_merge_recursive(from: $result[0], to : $result[count($result) -1])
如何在php中合并n个数组。我的意思是,我如何完成如下工作:array_merge(从:$result[0]到:$result[count($result)-1])或array_merge_recursivefrom(从:$result[0]到:$result[count($result)-1])
Where $result
is an array with multiple arrays inside it like this :
$result是一个数组,其中包含多个数组,如下所示:
$result = Array(
0 => array(),//associative array
1 => array(),//associative array
2 => array(),//associative array
3 => array()//associative array
)
My Result is :
我的结果是:
$result = Array(
0 => Array(
"name" => "Name",
"events" => 1,
"types" => 2
),
1 => Array(
"name" => "Name",
"events" => 1,
"types" => 3
),
2 => Array(
"name" => "Name",
"events" => 1,
"types" => 4
),
3 => Array(
"name" => "Name",
"events" => 2,
"types" => 2
),
4 => Array(
"name" => "Name",
"events" => 3,
"types" => 2
)
)
And what I need is
我需要的是
$result = Array(
"name" => "name",
"events" => array(1,2,3),
"types" => array(2,3,4)
)
4 个解决方案
#1
117
array_merge can take variable number of arguments, so with a little call_user_func_array trickery you can pass your $result
array to it:
array_merge可以使用可变数量的参数,因此使用一个小的call_user_func_array欺骗,您可以将$result数组传递给它:
$merged = call_user_func_array('array_merge', $result);
This basically run like if you would have typed:
这基本上就像你输入的:
$merged = array_merge($result[0], $result[1], .... $result[n]);
Update:
Now with 5.6, we have the ...
operator to unpack arrays to arguments, so you can:
现在有5。6,我们有。操作符将数组解压缩到参数中,所以可以:
$merged = array_merge(...$result);
And have the same results. *
结果是一样的。*
* The same results as long you have integer keys in the unpacked array, otherwise you'll get an E_RECOVERABLE_ERROR : type 4096 -- Cannot unpack array with string keys
error.
*只要在未打包的数组中有整数键,就会得到相同的结果,否则将得到E_RECOVERABLE_ERROR:类型4096——不能解压缩带有字符串键错误的数组。
#2
3
I really liked the answer from complex857 but it didn't work for me, because I had numeric keys in my arrays that I needed to preserve.
我非常喜欢complex857给出的答案,但它对我来说并不适用,因为我需要保存数组中的数字键。
I used the +
operator to preserve the keys (as suggested in PHP array_merge with numerical keys) and used array_reduce
to merge the array.
我使用+操作符来保存键(如PHP array_merge中的数字键),并使用array_reduce来合并数组。
So if you want to merge arrays inside an array while preserving numerical keys you can do it as follows:
如果你想在数组中合并数组同时保留数字键,你可以这样做:
<?php
$a = [
[0 => 'Test 1'],
[0 => 'Test 2', 2 => 'foo'],
[1 => 'Bar'],
];
print_r(array_reduce($a, function ($carry, $item) { return $carry + $item; }, []));
?>
Result:
结果:
Array
(
[0] => Test 1
[2] => foo
[1] => Bar
)
#3
2
If you would like to:
如果你想:
- check that each param going into array_merge is actually an array
- 检查进入array_merge的每个参数实际上是一个数组
- specify a particular property within one of the arrays to merge by
- 在要合并的数组中指定一个特定的属性
You can use this function:
你可以使用这个功能:
function mergeArrayofArrays($array, $property = null)
{
return array_reduce(
(array) $array, // make sure this is an array too, or array_reduce is mad.
function($carry, $item) use ($property) {
$mergeOnProperty = (!$property) ?
$item :
(is_array($item) ? $item[$property] : $item->$property);
return is_array($mergeOnProperty)
? array_merge($carry, $mergeOnProperty)
: $carry;
}, array()); // start the carry with empty array
}
Let's see it in action.. here's some data:
让我们看看它的行动。这里有一些数据:
Simple structure: Pure array of arrays to merge.
简单结构:纯数组合并。
$peopleByTypesSimple = [
'teachers' => [
0 => (object) ['name' => 'Ms. Jo', 'hair_color' => 'brown'],
1 => (object) ['name' => 'Mr. Bob', 'hair_color' => 'red'],
],
'students' => [
0 => (object) ['name' => 'Joey', 'hair_color' => 'blonde'],
1 => (object) ['name' => 'Anna', 'hair_color' => 'Strawberry Blonde'],
],
'parents' => [
0 => (object) ['name' => 'Mr. Howard', 'hair_color' => 'black'],
1 => (object) ['name' => 'Ms. Wendle', 'hair_color' => 'Auburn'],
],
];
Less simple: Array of arrays, but would like to specify the people and ignore the count.
不那么简单:数组,但想指定人员并忽略计数。
$peopleByTypes = [
'teachers' => [
'count' => 2,
'people' => [
0 => (object) ['name' => 'Ms. Jo', 'hair_color' => 'brown'],
1 => (object) ['name' => 'Mr. Bob', 'hair_color' => 'red'],
]
],
'students' => [
'count' => 2,
'people' => [
0 => (object) ['name' => 'Joey', 'hair_color' => 'blonde'],
1 => (object) ['name' => 'Anna', 'hair_color' => 'Strawberry Blonde'],
]
],
'parents' => [
'count' => 2,
'people' => [
0 => (object) ['name' => 'Mr. Howard', 'hair_color' => 'black'],
1 => (object) ['name' => 'Ms. Wendle', 'hair_color' => 'Auburn'],
]
],
];
Run it
运行它
$peopleSimple = mergeArrayofArrays($peopleByTypesSimple);
$people = mergeArrayofArrays($peopleByTypes, 'people');
Results - Both return this:
结果-两者都返回:
Array
(
[0] => stdClass Object
(
[name] => Ms. Jo
[hair_color] => brown
)
[1] => stdClass Object
(
[name] => Mr. Bob
[hair_color] => red
)
[2] => stdClass Object
(
[name] => Joey
[hair_color] => blonde
)
[3] => stdClass Object
(
[name] => Anna
[hair_color] => Strawberry Blonde
)
[4] => stdClass Object
(
[name] => Mr. Howard
[hair_color] => black
)
[5] => stdClass Object
(
[name] => Ms. Wendle
[hair_color] => Auburn
)
)
Extra Fun: If you want to single out one property in an array or object, like "name" from an array of people objects(or associate arrays), you can use this function
特别有趣的是:如果您想从一个数组或对象中挑选一个属性,比如“name”来自一个people对象数组(或关联数组),您可以使用这个函数
function getSinglePropFromCollection($propName, $collection, $getter = true)
{
return (empty($collection)) ? [] : array_map(function($item) use ($propName) {
return is_array($item)
? $item[$propName]
: ($getter)
? $item->{'get' . ucwords($propName)}()
: $item->{$propName}
}, $collection);
}
The getter is for possibly protected/private objects.
getter可能用于受保护/私有对象。
$namesOnly = getSinglePropFromCollection('name', $peopleResults, false);
$namesOnly = getSinglePropFromCollection('name', $peopleResults, false);
returns
返回
Array
(
[0] => Ms. Jo
[1] => Mr. Bob
[2] => Joey
[3] => Anna
[4] => Mr. Howard
[5] => Ms. Wendle
)
#4
0
Try this
试试这个
$result = array_merge($array1, $array2);
Or, instead of array_merge, you can use the + op which performs a union:
或者,您可以使用执行联合的+ op而不是array_merge:
$array2 + array_fill_keys($array1, '');
#1
117
array_merge can take variable number of arguments, so with a little call_user_func_array trickery you can pass your $result
array to it:
array_merge可以使用可变数量的参数,因此使用一个小的call_user_func_array欺骗,您可以将$result数组传递给它:
$merged = call_user_func_array('array_merge', $result);
This basically run like if you would have typed:
这基本上就像你输入的:
$merged = array_merge($result[0], $result[1], .... $result[n]);
Update:
Now with 5.6, we have the ...
operator to unpack arrays to arguments, so you can:
现在有5。6,我们有。操作符将数组解压缩到参数中,所以可以:
$merged = array_merge(...$result);
And have the same results. *
结果是一样的。*
* The same results as long you have integer keys in the unpacked array, otherwise you'll get an E_RECOVERABLE_ERROR : type 4096 -- Cannot unpack array with string keys
error.
*只要在未打包的数组中有整数键,就会得到相同的结果,否则将得到E_RECOVERABLE_ERROR:类型4096——不能解压缩带有字符串键错误的数组。
#2
3
I really liked the answer from complex857 but it didn't work for me, because I had numeric keys in my arrays that I needed to preserve.
我非常喜欢complex857给出的答案,但它对我来说并不适用,因为我需要保存数组中的数字键。
I used the +
operator to preserve the keys (as suggested in PHP array_merge with numerical keys) and used array_reduce
to merge the array.
我使用+操作符来保存键(如PHP array_merge中的数字键),并使用array_reduce来合并数组。
So if you want to merge arrays inside an array while preserving numerical keys you can do it as follows:
如果你想在数组中合并数组同时保留数字键,你可以这样做:
<?php
$a = [
[0 => 'Test 1'],
[0 => 'Test 2', 2 => 'foo'],
[1 => 'Bar'],
];
print_r(array_reduce($a, function ($carry, $item) { return $carry + $item; }, []));
?>
Result:
结果:
Array
(
[0] => Test 1
[2] => foo
[1] => Bar
)
#3
2
If you would like to:
如果你想:
- check that each param going into array_merge is actually an array
- 检查进入array_merge的每个参数实际上是一个数组
- specify a particular property within one of the arrays to merge by
- 在要合并的数组中指定一个特定的属性
You can use this function:
你可以使用这个功能:
function mergeArrayofArrays($array, $property = null)
{
return array_reduce(
(array) $array, // make sure this is an array too, or array_reduce is mad.
function($carry, $item) use ($property) {
$mergeOnProperty = (!$property) ?
$item :
(is_array($item) ? $item[$property] : $item->$property);
return is_array($mergeOnProperty)
? array_merge($carry, $mergeOnProperty)
: $carry;
}, array()); // start the carry with empty array
}
Let's see it in action.. here's some data:
让我们看看它的行动。这里有一些数据:
Simple structure: Pure array of arrays to merge.
简单结构:纯数组合并。
$peopleByTypesSimple = [
'teachers' => [
0 => (object) ['name' => 'Ms. Jo', 'hair_color' => 'brown'],
1 => (object) ['name' => 'Mr. Bob', 'hair_color' => 'red'],
],
'students' => [
0 => (object) ['name' => 'Joey', 'hair_color' => 'blonde'],
1 => (object) ['name' => 'Anna', 'hair_color' => 'Strawberry Blonde'],
],
'parents' => [
0 => (object) ['name' => 'Mr. Howard', 'hair_color' => 'black'],
1 => (object) ['name' => 'Ms. Wendle', 'hair_color' => 'Auburn'],
],
];
Less simple: Array of arrays, but would like to specify the people and ignore the count.
不那么简单:数组,但想指定人员并忽略计数。
$peopleByTypes = [
'teachers' => [
'count' => 2,
'people' => [
0 => (object) ['name' => 'Ms. Jo', 'hair_color' => 'brown'],
1 => (object) ['name' => 'Mr. Bob', 'hair_color' => 'red'],
]
],
'students' => [
'count' => 2,
'people' => [
0 => (object) ['name' => 'Joey', 'hair_color' => 'blonde'],
1 => (object) ['name' => 'Anna', 'hair_color' => 'Strawberry Blonde'],
]
],
'parents' => [
'count' => 2,
'people' => [
0 => (object) ['name' => 'Mr. Howard', 'hair_color' => 'black'],
1 => (object) ['name' => 'Ms. Wendle', 'hair_color' => 'Auburn'],
]
],
];
Run it
运行它
$peopleSimple = mergeArrayofArrays($peopleByTypesSimple);
$people = mergeArrayofArrays($peopleByTypes, 'people');
Results - Both return this:
结果-两者都返回:
Array
(
[0] => stdClass Object
(
[name] => Ms. Jo
[hair_color] => brown
)
[1] => stdClass Object
(
[name] => Mr. Bob
[hair_color] => red
)
[2] => stdClass Object
(
[name] => Joey
[hair_color] => blonde
)
[3] => stdClass Object
(
[name] => Anna
[hair_color] => Strawberry Blonde
)
[4] => stdClass Object
(
[name] => Mr. Howard
[hair_color] => black
)
[5] => stdClass Object
(
[name] => Ms. Wendle
[hair_color] => Auburn
)
)
Extra Fun: If you want to single out one property in an array or object, like "name" from an array of people objects(or associate arrays), you can use this function
特别有趣的是:如果您想从一个数组或对象中挑选一个属性,比如“name”来自一个people对象数组(或关联数组),您可以使用这个函数
function getSinglePropFromCollection($propName, $collection, $getter = true)
{
return (empty($collection)) ? [] : array_map(function($item) use ($propName) {
return is_array($item)
? $item[$propName]
: ($getter)
? $item->{'get' . ucwords($propName)}()
: $item->{$propName}
}, $collection);
}
The getter is for possibly protected/private objects.
getter可能用于受保护/私有对象。
$namesOnly = getSinglePropFromCollection('name', $peopleResults, false);
$namesOnly = getSinglePropFromCollection('name', $peopleResults, false);
returns
返回
Array
(
[0] => Ms. Jo
[1] => Mr. Bob
[2] => Joey
[3] => Anna
[4] => Mr. Howard
[5] => Ms. Wendle
)
#4
0
Try this
试试这个
$result = array_merge($array1, $array2);
Or, instead of array_merge, you can use the + op which performs a union:
或者,您可以使用执行联合的+ op而不是array_merge:
$array2 + array_fill_keys($array1, '');