0 votes

Sort multidimensional array by field

I don't quite understand the PHP documentation for computing a multidimensional array by a field. In my case I have the following array and I want to sort it by type, being associative:

array(4) {
  [0]=>
  array(90) {
    ["Juego"]=>
    string(29) "Sniper Ghost Warrior 2 - Gold"
    ["Tipologia"]=>
    string(11) "Complemento"
    ["Pertenece"]=>
    string(38) "66acd000-77fe-1000-9115-d802435907d4; "
  }
  [1]=>
  array(90) {
    ["Juego"]=>
    string(16) "MK14 EBR & skins"
    ["Tipologia"]=>
    string(11) "Juego"
    ["Pertenece"]=>
    string(38) "66acd000-77fe-1000-9115-d802435907d4; "
  }
  [2]=>
  array(90) {
    ["Juego"]=>
    string(15) "Limited Edition"
    ["Tipologia"]=>
    string(11) "Complemento"
    ["Pertenece"]=>
    string(38) "66acd000-77fe-1000-9115-d802435907d4; "
  }
  [4]=>
  array(90) {
    ["Juego"]=>
    string(15) "Snipter Ghost Warrior Pase de Temporada"
    ["Tipologia"]=>
    string(11) "Juego"
    ["Pertenece"]=>
    string(38) "66acd000-77fe-1000-9115-d802435957d4; "
  }

I want to see first those of the "Game" type, although I understand that you can choose between SORT_ASC o SORT_DESC .

0voto

amenadiel Points 14124

You can use ursort

Be your array:

$miarray = [
  [
    "Juego"=> "Sniper Ghost Warrior 2 - Gold",
    "Tipologia"=> "Complemento",
    "Pertenece"=> "66acd000-77fe-1000-9115-d802435907d4; ",
  ],
  [
    "Juego"=> "MK14 EBR & skins",
    "Tipologia"=> "Juego",
    "Pertenece"=> "66acd000-77fe-1000-9115-d802435907d4; ",
  ],
  [
    "Juego"=> "Limited Edition",
    "Tipologia"=> "Complemento",
    "Pertenece"=> "66acd000-77fe-1000-9115-d802435907d4; ",
  ],
  [
    "Juego"=> "Snipter Ghost Warrior Pase de Temporada",
    "Tipologia"=> "Juego",
    "Pertenece"=> "66acd000-77fe-1000-9115-d802435957d4; ",
  ]
];

You can sort the array with a self-defined function:

usort($miarray,  function ($a, $b) {

    if ($a['Tipologia'] == $b['Tipologia']) {
        return 0;
    }
    return ($a['Tipologia'] < $b['Tipologia']) ? 1 : -1;
});

The printing of the array by means of print_r($miarray); would throw

Array
(
    [0] => Array
        (
            [Juego] => MK14 EBR & skins
            [Tipologia] => Juego
            [Pertenece] => 66acd000-77fe-1000-9115-d802435907d4; 
        )

    [1] => Array
        (
            [Juego] => Snipter Ghost Warrior Pase de Temporada
            [Tipologia] => Juego
            [Pertenece] => 66acd000-77fe-1000-9115-d802435957d4; 
        )

    [2] => Array
        (
            [Juego] => Sniper Ghost Warrior 2 - Gold
            [Tipologia] => Complemento
            [Pertenece] => 66acd000-77fe-1000-9115-d802435907d4; 
        )

    [3] => Array
        (
            [Juego] => Limited Edition
            [Tipologia] => Complemento
            [Pertenece] => 66acd000-77fe-1000-9115-d802435907d4; 
        )

)

Depending on the function you define in ursort you can use more criteria for when the typologies are the same.

Fiddle: http://sandbox.onlinephpfunctions.com/code/22101943c5ebb9b56dd2d873c5290bf61a33b29b

HolaDevs.com

HolaDevs is an online community of programmers and software lovers.
You can check other people responses or create a new question if you don't find a solution

Powered by:

X