0

I have a question similar to this one: PHP Sort Array By SubArray Value however it is a bit different since I want to find/search for the highest of many subarray values and sort the "main" array after that. Below is the array structure I have, with groupings that contain a number of subitems where each subitem has a score.

Array
(
    [grouping1] => Array
        (
            [0] => Array
                (
                    [_score] => 8.2
                    [_source] => Array
                        (
                            [name] => Subitem 1111,
                            [ID => 999
                        )

                )

        )

    [grouping2] => Array
        (
            [0] => Array
                (
                    [_score] => 8.546042
                    [_source] => Array
                        (
                            [name] => Subitem2222,
                            [ID] => 987654
                        )

                )

        )

    [grouping3] => Array
        (
            [0] => Array
                (
                    [_score] => 10.163501
                    [_source] => Array
                        (
                            [name] => Subitem3333
                            [ID] => 12345

                        )

                )

            [1] => Array
                (
                    [_score] => 8.55
                    [_source] => Array
                        (
                            [name] => Subitem4444,
                            [ID] => 67890
                        )

                )

            [2] => Array
                (
                    [_score] => 9.55
                    [_source] => Array
                        (
                            [name] => Subitem5555
                            [ID] => 65421
                        )

                )

        )

)

I would like to

  1. Check the highest score amongst subitems for each grouping
  2. Sort groupings after highest available subitem score

The expected output is the containing array to have this order

  1. [0] grouping3 since its highest subarray score is 10.163501
  2. [1] grouping2 since its highest subarray score is 8.546042
  3. [2] grouping1 since its highest subarray score is 8.2
Oscar
  • 39
  • 1
  • 7

1 Answers1

0

Okay, that's what you need to do - find max score in each grouping and store a lookup [grouping -> score], then sort this lookup array preserving keys and build new array:

$lookup = [];
foreach ($source_array as $key => $grouping) {
    $max = 0;
    foreach ($grouping as $item) {
        if ($max < $item['_score']) {
            $max = $item['_score'];
        }
    }
    $lookup[$key] = $max;
}

// sort `$lookup` preserving keys
arsort($lookup);

$sorted_array = [];
foreach ($lookup as $key => $max) {
    $sorted_array[$key] = $source_array[$key];
}
u_mulder
  • 54,101
  • 5
  • 48
  • 64
  • I changed $sorted_array[$key] = $array[$key]; to $sorted_array[$key] = $source_array[$key]; and it seems to be working. Think it's a correct change? – Oscar Oct 18 '18 at 13:11