Sort multidimensional array by field

0

I do not quite understand the PHP documentation for computer a multidimensional array by a field. In my case I have the following array and I want to sort it by typology, 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 or SORT_DESC .

    
asked by JetLagFox 30.10.2017 в 22:10
source

1 answer

0

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 function defined by yourself:

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

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

Printing the array using 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: link

    
answered by 31.10.2017 в 00:46