如何对数组中包含在 JSON 中的值进行排序?

我正在接收来自数据库的数据,这些数据来自包含 JSON 的数组。我想按标题对重复的值进行分组,即 GROUP 是标题,BREED 是标题,NAME 是这个标题的描述,如下所示。

车辆(集团)

电动(品种)

奥迪 A3 Sportback

宝马i3

雪佛兰螺栓

克莱斯勒 Pacifica PHEV

福特聚变能源

现代 IONIQ Electric

梅赛德斯 GLE 550

福特

三菱/日产

自走式(BREED)

步兵战斗

装甲侦察

骑兵格斗

装甲人员

空中缆车

婴儿车

送货车

日产聆风

推土机

这是里面有 JSON 的数组,下面是获取信息的循环。

<?php

$array = array(

    '{

    "GROUP": "Vehicle",

    "BREED": "Self-Propelled",

    "NAME": "Infantry fighting."

    }',

    '{

        "GROUP": "Vehicle",

        "BREED": "Electric",

        "NAME": "Audi A3 Sportback."

    }',

    '{

        "GROUP": "Vehicle",

        "BREED": "Self-Propelled",

        "NAME": "Armored reconnaissance"

    }',

    '{

        "GROUP": "Vehicle",

        "BREED": "Electric",

        "NAME": "BMW i3"

    }',

    '{

        "GROUP": "Vehicle",

        "BREED": "Self-Propelled",

        "NAME": "Cavalry fighting"

    }',

    '{

        "GROUP": "Vehicle",

        "BREED": "Electric",

        "NAME": "Chevrolet Bolt"

    }',

    '{

        "GROUP": "Vehicle",

        "BREED": "Electric",

        "NAME": "Chrysler Pacifica PHEV."

    }',

    '{

        "GROUP": "Vehicle",

        "BREED": "Self-Propelled",

        "NAME": "Armored personnel"

    }',

    '{

        "GROUP": "Vehicle",

        "BREED": "Electric",

        "NAME": "Ford Fusion Energi."

    }',

    '{

        "GROUP": "Vehicle",

        "BREED": "Self-Propelled",

        "NAME": "aerial tramway"

    }',

    '{

        "GROUP": "Vehicle",

        "BREED": "Self-Propelled",

        "NAME": "baby carriage"

    }',

    '{

        "GROUP": "Vehicle",

        "BREED": "Electric",

        "NAME": "Hyundai IONIQ Electric."

    }',

    '{

        "GROUP": "Vehicle",

        "BREED": "Self-Propelled",

        "NAME": "delivery truck"

    }',

    '{

        "GROUP": "Vehicle",

        "BREED": "Electric",

        "NAME": "Mercedes GLE 550e."

    }',

    '{

        "GROUP": "Vehicle",

        "BREED": "Electric",

        "NAME": "Ford F"

    }',


注意 BREED 和 GROUP 一遍又一遍地重复,我怎样才能让它们看起来像顶级模型?


函数式编程
浏览 209回答 2
2回答

森林海

再次阅读文章后,我意识到,有是在那里尝试已经(对不起,我先前的评论说,否则......)。关于如何解决数据重组问题,我的建议如下:$newarr=array();for($i = 0; $i < count($array); $i++){&nbsp;&nbsp;&nbsp; $obj = json_decode($array[$i]);&nbsp; $newarr[$obj->GROUP][$obj->BREED][]=$obj->NAME;}foreach ($newarr as $group => $breeds) {&nbsp; &nbsp; echo "<h1>$group</h1>\n";&nbsp; &nbsp;&nbsp;&nbsp; &nbsp; foreach ($breeds as $breed => $vehicles)&nbsp;&nbsp; &nbsp; &nbsp; echo "<p><b>$breed</b><br>\n".join("<br>\n",$vehicles)."</p>\n";}您可以在此处找到工作演示:https : //rextester.com/OYD48391

泛舟湖上清波郎朗

您需要先重构您的数据,然后再渲染它们,如下所示// we will restructure the array to look like the following[&nbsp; &nbsp; "Vehicle" => [&nbsp; &nbsp; &nbsp; &nbsp; "Self-Propelled" => [&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; 0 => "Infantry fighting.",&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; 1 => "Cavalry fighting",&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; 2 => "Armored personnel",&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; 3 => "aerial tramway",&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; 4 => "baby carriage",&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; 5 => "delivery truck",&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; 6 => "Nissan LEAF.",&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; 7 => "Earth mover",&nbsp; &nbsp; &nbsp; &nbsp; ],&nbsp; &nbsp; &nbsp; &nbsp;&nbsp;&nbsp; &nbsp; &nbsp; &nbsp; "Electric" => [&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; 0 => "Audi A3 Sportback.",&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; 1 => "BMW i3",&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; 2 => "Chevrolet Bolt",&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; 3 => "Chrysler Pacifica PHEV.",&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; 4 => "Ford Fusion Energi.",&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; 5 => "Hyundai IONIQ Electric.",&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; 6 => "Mercedes GLE 550e.",&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; 7 => "Ford F",&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; 8 => "Mitsubishi/Nissan",&nbsp; &nbsp; &nbsp; &nbsp; ]&nbsp; &nbsp; ],&nbsp; &nbsp; "Passageiro" => [&nbsp; &nbsp; &nbsp; &nbsp; "Self-Propelled" => [&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; 0 => "Armored reconnaissance",&nbsp; &nbsp; &nbsp; &nbsp; ]&nbsp; &nbsp; ],]// first restructure the array$restructuredArray = [];foreach ($array as $jsonData) {&nbsp; &nbsp; $data = json_decode($jsonData);&nbsp; &nbsp; $restructuredArray[$data->GROUP] = $restructuredArray[$data->GROUP] ?? [];&nbsp; &nbsp; $restructuredArray[$data->GROUP][$data->BREED] = $restructuredArray[$data->GROUP][$data->BREED] ?? [];&nbsp; &nbsp; $restructuredArray[$data->GROUP][$data->BREED][] = $data->NAME;}// second rendering the restructured arrayforeach ($restructuredArray as $group => $breeds) {&nbsp; &nbsp; echo "<h1>{$group}</h1>";&nbsp; &nbsp; foreach($breeds as $breed => $names) {&nbsp; &nbsp; &nbsp; &nbsp; echo "<h3>{$breed}</h3>";&nbsp; &nbsp; &nbsp; &nbsp; foreach($names as $name) {&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; echo "<p>{$name}</p>";&nbsp; &nbsp; &nbsp; &nbsp; }&nbsp; &nbsp; }}建议 如果您可以控制数据库,最好以更好的格式或结构保存数据,在您的情况下,您可以使用 SQL(如 MySQL 使用关系)或 NoSQL(基于文档的数据库,如 MongoDB),两者都适合为您的需要。
打开App,查看更多内容
随时随地看视频慕课网APP