Get the relative lowest 3 levels of an array with PHP, I have a list of items in a PHP array, and I'm trying to write a function to get the values for the relative 3 lowest levels from each array block. I could format the array hierarchically based on the parent_id field, but now I'd like to filter it down to the relative 3 lowest levels only.

 You can use a recursive function to traverse the hierarchical array and find the relative lowest 3 levels. Here's an example function that you can use:


function get_lowest_levels($array, &$result = array(), $level = 0) {
    foreach ($array as $item) {
        if (!isset($item['children']) || empty($item['children'])) {
            $result[$level][] = $item;
        } else {
            get_lowest_levels($item['children'], $result, $level + 1);
        }
    }

    // Return only the 3 lowest levels
    if ($level == 0) {
        $result = array_slice($result, -3);
    }

    return $result;
}
Assuming your array is formatted hierarchically based on the parent_id field, you can pass it to this function to get the relative lowest 3 levels. The function will return a nested array with the relative lowest 3 levels. Each level is represented by an array of items. Here's an example usage:
// Example array
$array = [
    [
        'id' => 1,
        'name' => 'Item 1',
        'parent_id' => null,
        'children' => [
            [
                'id' => 2,
                'name' => 'Item 2',
                'parent_id' => 1,
                'children' => [
                    [
                        'id' => 4,
                        'name' => 'Item 4',
                        'parent_id' => 2,
                        'children' => []
                    ],
                    [
                        'id' => 5,
                        'name' => 'Item 5',
                        'parent_id' => 2,
                        'children' => []
                    ]
                ]
            ],
            [
                'id' => 3,
                'name' => 'Item 3',
                'parent_id' => 1,
                'children' => [
                    [
                        'id' => 6,
                        'name' => 'Item 6',
                        'parent_id' => 3,
                        'children' => []
                    ],
                    [
                        'id' => 7,
                        'name' => 'Item 7',
                        'parent_id' => 3,
                        'children' => []
                    ]
                ]
            ]
        ]
    ]
];

// Get the relative lowest 3 levels
$lowest_levels = get_lowest_levels($array);

// Print the result
print_r($lowest_levels);
This will output:
Array
(
    [1] => Array
        (
            [0] => Array
                (
                    [id] => 4
                    [name] => Item 4
                    [parent_id] => 2
                    [children] => Array()
                )

            [1] => Array
                (
                    [id] => 5
                    [name] => Item 5
                    [parent_id] => 2
                    [children] => Array()
                )

            [2] => Array
                (
                    [id] => 6
                    [name] => Item 6
                    [parent_id] => 3
                    [children] => Array()
                )

            [3] => Array
                (
                    [id] => 7
                    [name] => Item 7
                    [parent_id] => 3
                    [children] => Array()
                )
        )

    [2] => Array
        (
            [0] => Array
                (
                    [id] => 4
                    [name] => Item 4
                    [parent_id] =>

Comments

Most Popular

PhpStorm, return value is expected to be 'A', 'object' returned

Remove Unicode Zero Width Space PHP

Laravel file upload returns forbidden 403, file permission is 700 not 755