PHP中具有两个排序条件的多维json数组

时间:2019-11-12 17:50:28

标签: php arrays json

所以我有一个看起来像https://api.myjson.com/bins/zux0q(也是下面的文字)的json文件

{"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"}]}

现在,我想按OEM字母顺序然后按键名称进行排序。
我尝试了一些代码,但只能按键名进行排序,并且用尽了所有想法。 我需要一些多维数组排序。 像这样:

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:...

我们非常感谢您的帮助!

1 个答案:

答案 0 :(得分:0)

假设您已经阅读了json文件,并将其内容转换为如下所示的PHP数组:

$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',
    ),
  ),
);

然后,您可以按oemname对项目进行分组,如下所示:

$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>");

这将返回以下结果:

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
                )

        )

)