-1

So I have a json file that looks like so https://api.myjson.com/bins/zux0q (also text below)

{"beryllium":[{"oem":"Xiaomi","maintainer":"Lup Gabriel ","nick":"gwolfu","crversion":"6.0","builddate":"20191110","download":"https://url","forum":"https://url"}],"cheeseburger":[{"oem":"OnePlus","maintainer":"Pranav Vashi ","nick":"neobuddy89","crversion":"6.0","builddate":"20191107","download":"https://url","forum":"https://url"}],"enchilada":[{"oem":"OnePlus","maintainer":"Hildo Boerboom ","nick":"firebird11","crversion":"6.0","builddate":"20191102","download":"https://url","forum":"https://url"}],"fajita":[{"oem":"OnePlus","maintainer":"Hildo Boerboom ","nick":"firebird11","crversion":"6.0","builddate":"20191102","download":"https://url","forum":"https://url"}],"guacamole":[{"oem":"OnePlus","maintainer":"Lup Gabriel ","nick":"gwolfu","crversion":"6.0","builddate":"20191110","download":"https://url","forum":"https://url"}],"oneplus2":[{"oem":"OnePlus","maintainer":"Lucian Iordache ","nick":"lucyr03","crversion":"6.0","builddate":"20191103","download":"https://url","forum":"https://url"}]}

Now I'd like to sort alphabetically by OEM and then by key name.
I tried some code but can only make it sort by key name and ran out of ideas . I need some multidimensional array sort I guess. So something like this:

OnePlus =>> 
        cheeseburger =>>
                     maintainer:...
                     nick:...
                     crversion:...
                     builddate:...
                     download:...
                     forum:...
        enchilada ==>
                     maintainer:...
                     nick:...
                     crversion:...
                     builddate:...
                     download:...
                     forum:...
        fajita ==>
                     maintainer:...
                     nick:...
                     crversion:...
                     builddate:...
                     download:...
                     forum:...
        guacamole ==>
                     maintainer:...
                     nick:...
                     crversion:...
                     builddate:...
                     download:...
                     forum:...
        oneplus2 ==>
                     maintainer:...
                     nick:...
                     crversion:...
                     builddate:...
                     download:...
                     forum:...
Xiaomi ==>
        beryllium ==>
                     maintainer:...
                     nick:...
                     crversion:...
                     builddate:...
                     download:...
                     forum:...

Any help is highly appreciated!

theduck
  • 2,589
  • 13
  • 17
  • 23
  • 1
    Does this answer your question? [Sort multidimensional array by multiple keys](https://stackoverflow.com/questions/3232965/sort-multidimensional-array-by-multiple-keys) – Progrock Nov 12 '19 at 18:34

1 Answers1

0

Assuming that you already read your json file and converted its content to a PHP array like below:

$input = array (
  'beryllium' => 
  array (
    0 => 
    array (
      'oem' => 'Xiaomi',
      'maintainer' => 'Lup Gabriel ',
      'nick' => 'gwolfu',
      'crversion' => '6.0',
      'builddate' => '20191110',
      'download' => 'https://url',
      'forum' => 'https://url',
    ),
  ),
  'cheeseburger' => 
  array (
    0 => 
    array (
      'oem' => 'OnePlus',
      'maintainer' => 'Pranav Vashi ',
      'nick' => 'neobuddy89',
      'crversion' => '6.0',
      'builddate' => '20191107',
      'download' => 'https://url',
      'forum' => 'https://url',
    ),
  ),
  'enchilada' => 
  array (
    0 => 
    array (
      'oem' => 'OnePlus',
      'maintainer' => 'Hildo Boerboom ',
      'nick' => 'firebird11',
      'crversion' => '6.0',
      'builddate' => '20191102',
      'download' => 'https://url',
      'forum' => 'https://url',
    ),
  ),
  'fajita' => 
  array (
    0 => 
    array (
      'oem' => 'OnePlus',
      'maintainer' => 'Hildo Boerboom ',
      'nick' => 'firebird11',
      'crversion' => '6.0',
      'builddate' => '20191102',
      'download' => 'https://url',
      'forum' => 'https://url',
    ),
  ),
  'guacamole' => 
  array (
    0 => 
    array (
      'oem' => 'OnePlus',
      'maintainer' => 'Lup Gabriel ',
      'nick' => 'gwolfu',
      'crversion' => '6.0',
      'builddate' => '20191110',
      'download' => 'https://url',
      'forum' => 'https://url',
    ),
  ),
  'oneplus2' => 
  array (
    0 => 
    array (
      'oem' => 'OnePlus',
      'maintainer' => 'Lucian Iordache ',
      'nick' => 'lucyr03',
      'crversion' => '6.0',
      'builddate' => '20191103',
      'download' => 'https://url',
      'forum' => 'https://url',
    ),
  ),
);

Then, you can group your items by oem and by name like below:

$records = [];
foreach($input as $key => $value){
  $brand = $value[0]['oem'];
  unset($value[0]['oem']);
  $records[$brand][$key] = $value[0];
}
ksort($records);
print("<pre>" . print_r($records, true) . "</pre>");

This will return the following result:

Array
(
    [OnePlus] => Array
        (
            [cheeseburger] => Array
                (
                    [maintainer] => Pranav Vashi 
                    [nick] => neobuddy89
                    [crversion] => 6.0
                    [builddate] => 20191107
                    [download] => https://url
                    [forum] => https://url
                )

            [enchilada] => Array
                (
                    [maintainer] => Hildo Boerboom 
                    [nick] => firebird11
                    [crversion] => 6.0
                    [builddate] => 20191102
                    [download] => https://url
                    [forum] => https://url
                )

            [fajita] => Array
                (
                    [maintainer] => Hildo Boerboom 
                    [nick] => firebird11
                    [crversion] => 6.0
                    [builddate] => 20191102
                    [download] => https://url
                    [forum] => https://url
                )

            [guacamole] => Array
                (
                    [maintainer] => Lup Gabriel 
                    [nick] => gwolfu
                    [crversion] => 6.0
                    [builddate] => 20191110
                    [download] => https://url
                    [forum] => https://url
                )

            [oneplus2] => Array
                (
                    [maintainer] => Lucian Iordache 
                    [nick] => lucyr03
                    [crversion] => 6.0
                    [builddate] => 20191103
                    [download] => https://url
                    [forum] => https://url
                )

        )

    [Xiaomi] => Array
        (
            [beryllium] => Array
                (
                    [maintainer] => Lup Gabriel 
                    [nick] => gwolfu
                    [crversion] => 6.0
                    [builddate] => 20191110
                    [download] => https://url
                    [forum] => https://url
                )

        )

)
Vasilis G.
  • 7,556
  • 4
  • 19
  • 29
  • while this is close to what I want, wanted to also sort so that OnePlus is first and Xiaomi is second (alphabetically) – Lup Gabriel Nov 12 '19 at 18:18