Reputation: 1183
I'm trying to merge a collection of nested array elements into one array.
Array:
crop_data = [
[
["crop" => "soy"] // 0
],
[
["crop" => "rye"] // 1
],
[
["crop" => "tree"] // 2
]
],
[
[
["crop" => "salt"] // 0
],
[
["crop" => "farm"] // 1
]
],
[
[
["year" => "2015"]
]
]
I've tried the following...
$crop_data = array(); // new array
foreach($crop_list as $value) {
$crop_data = array_merge($value, $crop_list));
}
I would like to merge the inner elements of the three arrays into one array. Any tips on how to achieve this?
Upvotes: 0
Views: 69
Reputation: 41810
You can use array_walk_recursive for this.
$merged = array();
array_walk_recursive($crop_data, function($v, $k) use (&$merged) {
$merged[$k][] = $v;
});
Upvotes: 1