2

How can I sort this array by the sum value of each set in the deepest nested array?

$arr = Array(
    Array(
        'I am the first string.',
        Array(
            Array( 'round', 1, 1 ),
            Array( 'rhythm', 1, 1 )
        )
    ),
    Array(
        'I am the second string',
        Array(
            Array( 'cps', 1, 1 ),
            Array( 'Hz', 1, 3 ),
            Array( 'hertz', 1, 1 )
        )
    ),
    Array(
        'I am the third string.',
        Array(
            Array( 'uucps', 1, 1 ),
            Array( 'uuHz', 1, 2 ),
            Array( 'uuhertz', 1, 1 )
        )
    )

);

I need to sort the second dimension of arrays by the sum value of third key of the deepest set(third dimension).

So the end result will look like this:

$arr = Array(
    Array(
        'I am the second string',
        Array(
            Array( 'cps', 1, 1 ),
            Array( 'Hz', 1, 3 ),
            Array( 'hertz', 1, 1 )
        )
    ),
    Array(
        'I am the third string.',
        Array(
            Array( 'uucps', 1, 1 ),
            Array( 'uuHz', 1, 2 ),
            Array( 'uuhertz', 1, 1 )
        )
    ),
    Array(
        'I am the first string.',
        Array(
            Array( 'round', 1, 1 ),
            Array( 'rhythm', 1, 1 )
        )
    )

);

I know that usort might be my best bet but I can't seems to make it work for my use case.

1
  • 1
    It might be helpful if you could include what you have already tried Commented May 9, 2016 at 20:10

1 Answer 1

3

The solution using usort, array_sum and array_column functions:

usort($arr, function($a, $b){
    $prev = array_sum(array_column($a[1], 2)); // summing up values for each third key (2)
    $next = array_sum(array_column($b[1], 2));

    return $next - $prev;    
});

print_r($arr);

The output:

Array
(
    [0] => Array
        (
            [0] => I am the second string
            [1] => Array
                (
                    [0] => Array
                        (
                            [0] => cps
                            [1] => 1
                            [2] => 1
                        )
                    [1] => Array
                        (
                            [0] => Hz
                            [1] => 1
                            [2] => 3
                        )
                    [2] => Array
                        (
                            [0] => hertz
                            [1] => 1
                            [2] => 1
                        )
                )
        )
    [1] => Array
        (
            [0] => I am the third string.
            [1] => Array
                (
                    [0] => Array
                        (
                            [0] => uucps
                            [1] => 1
                            [2] => 1
                        )
                    [1] => Array
                        (
                            [0] => uuHz
                            [1] => 1
                            [2] => 2
                        )
                    [2] => Array
                        (
                            [0] => uuhertz
                            [1] => 1
                            [2] => 1
                        )
                )
        )
    [2] => Array
        (
            [0] => I am the first string.
            [1] => Array
                (
                    [0] => Array
                        (
                            [0] => round
                            [1] => 1
                            [2] => 1
                        )
                    [1] => Array
                        (
                            [0] => rhythm
                            [1] => 1
                            [2] => 1
                        )
                )
        )
)
Sign up to request clarification or add additional context in comments.

1 Comment

Thank you very much. I guess I was missing the knowledge of array_column. I was trying to sum without it and it was obviously wasn't working because I was not really passing an array to it but one value at a time as it was in the loop. Thank you again.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.