note 124471 added to function.array-reduce
| From: | souzacomprog at gmail dot com | Date: | Fri, 06 Dec 2019 00:16:01 +0000 |
| Subject: | note 124471 added to function.array-reduce | ||
| Groups: | php.notes | ||
| Request: | Send a blank email to php-notes+get-213385@lists.php.net to get a copy of this message | ||
Sometimes we need to go through an array and group the indexes so that it is easier and easier to
extract them in the iteration.
<?php
$people = [
['id' => 1, 'name' => 'Hayley'],
['id' => 2, 'name' => 'Jack', 'dad' => 1],
['id' => 3, 'name' => 'Linus', 'dad'=> 4],
['id' => 4, 'name' => 'Peter' ],
['id' => 5, 'name' => 'Tom', 'dad' => 4],
];
$family = array_reduce($people, function($accumulator, $item) {
// if you don't have a dad you are probably a dad
if (!isset($item['dad'])) {
$id = $item['id'];
$name = $item['name'];
// take the children if you already have
$children = $accumulator[$id]['children'] ?? [];
// add dad
$accumulator[$id] = ['id' => $id, 'name' =>
$name,'children' => $children];
return $accumulator;
}
// add a new dad if you haven't already
$dad = $item['dad'];
if (!isset($accumulator[$dad])) {
// how did you find the dad will first add only with children
$accumulator[$dad] = ['children' => [$item]];
return $accumulator;
}
// add a son to his dad who has already been added
// by the first or second conditional "if"
$accumulator[$dad]['children'][] = $item;
return $accumulator;
}, []);
var_export(array_values($family));
?>
OUTPUT
array (
0 =>
array (
'id' => 1,
'name' => 'Hayley',
'children' =>
array (
0 =>
array (
'id' => 2,
'name' => 'Jack',
'dad' => 1,
),
),
),
1 =>
array (
'id' => 4,
'name' => 'Peter',
'children' =>
array (
0 =>
array (
'id' => 3,
'name' => 'Linus',
'dad' => 4,
),
1 =>
array (
'id' => 5,
'name' => 'Tom',
'dad' => 4,
),
),
),
)
<?php
$array = [
[
"menu_id" => "1",
"menu_name" => "Clients",
"submenu_name" => "Add",
"submenu_link" => "clients/add"
],
[
"menu_id" => "1",
"menu_name" => "Clients",
"submenu_name" => "List",
"submenu_link" => "clients"
],
[
"menu_id" => "2",
"menu_name" => "Products",
"submenu_name" => "List",
"submenu_link" => "products"
],
];
//Grouping submenus to their menus
$menu = array_reduce($array, function($accumulator, $item){
$index = $item['menu_name'];
if (!isset($accumulator[$index])) {
$accumulator[$index] = [
'menu_id' => $item['menu_id'],
'menu_name' => $item['menu_name'],
'submenu' => []
];
}
$accumulator[$index]['submenu'][] = [
'submenu_name' => $item['submenu_name'],
'submenu_link' => $item['submenu_link']
];
return $accumulator;
}, []);
var_export(array_values($menu));
?>
OUTPUT
array (
0 =>
array (
'menu_id' => '1',
'menu_name' => 'Clients',
'submenu' =>
array (
0 =>
array (
'submenu_name' => 'Add',
'submenu_link' => 'clients/add',
),
1 =>
array (
'submenu_name' => 'List',
'submenu_link' => 'clients',
),
),
),
1 =>
array (
'menu_id' => '2',
'menu_name' => 'Products',
'submenu' =>
array (
0 =>
array (
'submenu_name' => 'List',
'submenu_link' => 'products',
),
),
),
)
----
Server IP: 208.43.231.9
Probable Submitter: 82.199.157.54 (proxied: 177.74.220.109)
----
Manual Page -- http://php.net/manual/en/function.array-reduce.php
Edit -- https://master.php.net/note/edit/124471
Del: integrated -- https://master.php.net/note/delete/124471/integrated
Del: useless -- https://master.php.net/note/delete/124471/useless
Del: bad code -- https://master.php.net/note/delete/124471/bad+code
Del: spam -- https://master.php.net/note/delete/124471/spam
Del: non-english -- https://master.php.net/note/delete/124471/non-english
Del: in docs -- https://master.php.net/note/delete/124471/in+docs
Del: other reasons-- https://master.php.net/note/delete/124471
Reject -- https://master.php.net/note/reject/124471
Search -- https://master.php.net/manage/user-notes.php