0

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);

?>
Alex
  • 25
  • 2

1 Answers1

0

From https://stackoverflow.com/a/8587437/476:

$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
    ]
);

function buildTree(array $elements, $parentId = 0) {
    $branch = array();

    foreach ($elements as $element) {
        if ($element['SECTION_ID'] == $parentId) {
            $children = buildTree($elements, $element['ID']);
            if ($children) {
                $element['SECTIONS'] = $children;
            }
            $branch[] = $element;
        }
    }

    return $branch;
}

$sectionIds = array_reduce($info, function($carry, $element) {
    $carry[] = $element['SECTION_ID'];
    return $carry;
}, []);
sort($sectionIds);

$tree = buildTree($info, $sectionIds[0] ?? 0);

var_export($tree);
vonschlager
  • 324
  • 1
  • 6