G
G
gistol2015-10-07 09:48:39
PHP
gistol, 2015-10-07 09:48:39

How to sort an array in a given order?

Greetings.
There is an array:

Array
(
    [1] => Array
        (
            [Высшая] => 0
            [Вторая] => 0
            [Первая] => 0
        )

    [2] => Array
        (
            [Вторая] => 0
            [Первая] => 0
            [Высшая] => 0
        )

    [3] => Array
        (
            [Высшая] => 0
            [Первая] => 0
            [Вторая] => 0
        )

    [4] => Array
        (
            [Высшая] => 0
            [Первая] => 0
            [Вторая] => 0
        )

    [5] => Array
        (
            [Вторая] => 0
            [Высшая] => 0
            [Первая] => 0
        )

)

I want leagues to always be sorted in the following order $league_sort_order = array('0'=>'highest', '1'=>'first', '2'=>'second');
Tell me how to sort an array according to a given condition.

Answer the question

In order to leave comments, you need to log in

2 answer(s)
G
gistol, 2015-10-07
@gistol

I solved it clumsily, but it works:

$league_sort_order = array('Высшая', 'Первая', 'Вторая');
    foreach ($league_sort_order as $league_sort_order_key => $league_sort_order_value) {
      foreach ($tournaments_array as $tour_number_key => $tour_number_value) {
        foreach ($tour_number_value as $league_key => $league_value) {
          if ($league_value['players']) {
            if ($league_sort_order_value == $league_key) {
              foreach ($league_value['players'] as $player) {
                usort($league_value['players'], "cmp_total");
                $tournaments_array_sorted_by_league[$tour_number_key][$league_sort_order_value] = $league_value;
                //$tournaments_array[$tour_number_key][$league_sort_order_value] = $league_value;
              }
            }
          }
        }
      }
    }

A
Alexey Ukolov, 2015-10-07
@alexey-m-ukolov

en2.php.net/manual/en/function.uksort.php

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question