Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- <?php
- $tagged_fruits = [
- [
- '11' => 'apple',
- '12' => 'banana',
- '13' => 'cherry',
- ],
- [
- '14' => 'pear',
- '15' => 'apricot',
- '16' => 'plum',
- ],
- [
- '17' => 'grapes',
- '18' => 'peach',
- '19' => 'melon',
- ],
- [
- '20' => 'strawberry',
- ],
- ];
- $foreach = [];
- foreach($tagged_fruits as $fruits) {
- foreach($fruits as $id => $fruit) {
- $foreach[$id] = $fruit;
- }
- }
- $walked = [];
- \array_walk($tagged_fruits, function ($fruits) use (&$walked) {
- $walked += $fruits;
- });
- \var_dump([
- 'tagged' => $tagged_fruits,
- 'merged' => \array_merge(...$tagged_fruits),
- 'walked' => $walked,
- 'reduced' => \array_reduce($tagged_fruits, function($reduced, $fruits){
- $reduced = is_array($reduced) ? ($reduced + $fruits) : $fruits;
- return $reduced;
- }),
- 'recursive' => \array_merge_recursive(...$tagged_fruits),
- 'foreach' => $foreach,
- ]);
Add Comment
Please, Sign In to add comment