Как сортировать массив по значению ключа

Q_BASIC

Хранитель порядка
Регистрация
30 Ноя 2013
Сообщения
516
Реакции
1.240
Надо получить массив с самым большим ключем. Массив:
Код:
Array
(
    [1280] => Array
        (
            [0] => mp4
            [1] => 1280x720
        )

    [640] => Array
        (
            [0] => mp4
            [1] => 640x360
        )

    [320] => Array
        (
            [0] => 3gp
            [1] => 320x240
        )

    [176] => Array
        (
            [0] => 3gp
            [1] => 176x144
        )

)

И надо получить массив:
Код:
 [1280] => Array
        (
            [0] => mp4
            [1] => 1280x720
        )

Пробовал sort, rsort и вот что получилось:
Код:
Array
(
    [0] => Array
        (
            [0] => mp4
            [1] => 640x360
        )

    [1] => Array
        (
            [0] => mp4
            [1] => 1280x720
        )

    [2] => Array
        (
            [0] => 3gp
            [1] => 320x240
        )

    [3] => Array
        (
            [0] => 3gp
            [1] => 176x144
        )

)
 
PHP:
$myArray = [
    '1280' => ['mp4','1280x720'],
    '640' => ['mp4','640x360'],
    '320' => ['3gp','320x240'],
    '176' => ['3gp','176x144']

];
usort($myArray, function($a, $b) {
    return $a[1] - $b[1];
});
var_dump($myArray);
Выдаст
Код:
array(4) {
  [0] =>
  array(2) {
    [0] =>
    string(3) "3gp"
    [1] =>
    string(7) "176x144"
  }
  [1] =>
  array(2) {
    [0] =>
    string(3) "3gp"
    [1] =>
    string(7) "320x240"
  }
  [2] =>
  array(2) {
    [0] =>
    string(3) "mp4"
    [1] =>
    string(7) "640x360"
  }
  [3] =>
  array(2) {
    [0] =>
    string(3) "mp4"
    [1] =>
    string(8) "1280x720"
  }
}
 
PHP:
function getBigest($array)
{
      $bigestKey = max(array_keys($array));
     return $array[$bigestKey];
}
 
Назад
Сверху