There is an array with values:
$info = array(
[ 'ID' => 90,
'NAME' => 'ITEM',
'PAGE_URL' => '/cat/item/',
'SECTION_ID' => '78'],
[
'ID' => 98,
'NAME' => 'ITEM2',
'PAGE_URL' => '/cat/item2/',
'SECTION_ID' => 90
],
[
'ID' => 328,
'NAME' => 'ITEM3',
'PAGE_URL' => '/cat/ITEM3/',
'SECTION_ID' => 90
],
[
'ID' => 91,
'NAME' => 'item123123',
'PAGE_URL' => '/cat/item123123/',
'SECTION_ID' => 78
],
[
'ID' => 421,
'NAME' => 'item12',
'PAGE_URL' => '/cat/item12/',
'SECTION_ID' => 98
]
)
It was necessary to make it so that if SECTION_ID coincides with ID the value looked like and I made this moment myself:
[0] => Array
(
[ID] => 90
[NAME] => ITEM
[PAGE_URL] => /cat/item/
[SECTION_ID] => 78
[SECTIONS] => Array
(
[0] => Array
(
[ID] => 98
[NAME] => ITEM2
[PAGE_URL] => /cat/item2/
[SECTION_ID] => 90
)
[1] => Array
(
[ID] => 328
[NAME] => ITEM3
[PAGE_URL] => /cat/ITEM3/
[SECTION_ID] => 90
)
)
)
[1] => Array (
(
[ID] => 91
[NAME] => item123123
[PAGE_URL] => /cat/item123123/
[SECTION_ID] => 78
)
)
I just did not take into account one point and can not implement it, if the internal values of SECTION have matches by ID and SECTION_ID, then you need them to also be inside those values where there are matches
[0] => Array
(
[ID] => 90
[NAME] => ITEM
[PAGE_URL] => /cat/item/
[SECTION_ID] => 78
[SECTIONS] => Array
(
[0] => Array
(
[ID] => 98
[NAME] => ITEM2
[PAGE_URL] => /cat/item2/
[SECTION_ID] => 90
[SUB_SECTION] => Array(
[0] => Array(
[ID] => 421
[NAME] => item12
[PAGE_URL] => /cat/item12/
[SECTION_ID] => 98
)
)
)
[1] => Array
(
[ID] => 328
[NAME] => ITEM3
[PAGE_URL] => /cat/ITEM3/
[SECTION_ID] => 90
)
)
)
...
The code I wrote:
<?
foreach($info as &$parent) {
$parent['SECTIONS'] = [];
foreach($info as &$child) {
if ($parent['ID'] === $child['SECTION_ID']) {
$parent['SECTIONS'][] = $child;
$child['nested'] = true;
}
}
unset($child);
}
unset($parent);
$result = array_filter($info, function($value) {
return !isset($value['nested']) || !$value['nested'];
});
print_r($result);
?>