【问题标题】:Multidimentional json array with two sort conditions in php [duplicate]php中具有两个排序条件的多维json数组[重复]
【发布时间】:2020-03-08 11:33:41
【问题描述】:

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

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

非常感谢任何帮助!

【问题讨论】:

标签: php arrays json


【解决方案1】:

假设您已经读取了 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
                )

        )

)

【讨论】:

  • 虽然这接近我想要的,但也想排序,以便一加第一,小米第二(按字母顺序)
猜你喜欢
  • 2015-10-20
  • 2015-12-20
  • 1970-01-01
  • 2019-12-28
  • 1970-01-01
  • 2020-04-14
  • 1970-01-01
  • 2020-03-20
  • 1970-01-01
相关资源
最近更新 更多