0

I've found this previous question that is quite similar to what I'm trying to achieve but I'd like to do it differently. I have an array of menu items which I've sorted out to be as a tree with the count of children.

$totalCount = 7; //defined in some previous code
Array
(
    [0] => Array
        (
            [count] => 1
        )
    [1] => Array
        (
            [count] => 1
        )
    [2] => Array
        (
            [count] => 1
        )
    [3] => Array
        (
            [count] => 2
        )
    [4] => Array
        (
            [count] => 2
        )
    )

)

I'd like to be able to sort this array into three arrays that are as even as possible and based on the number in count. The only way I can think of doing this is by a giant recursive loop. Is there a much simpler way of doing this that I'm overlooking like the for loop in the previous question?

edit 1: Expected output:

    Array
(
    [0] => Array 
        (

        [0] => Array
            (
                [count] => 1
            )
        [1] => Array
            (
                [count] => 1
            )
        )
    [1] => Array
        (
            [3] => Array
            (
                [count] => 2
            )
        )
    [2] => Array
        (
            [2] => Array
                (
                    [count] => 1
                )
            [4] => Array
                (
                    [count] => 2
                )
        )
)
Community
  • 1
  • 1
Blakethepatton
  • 537
  • 6
  • 17

0 Answers0