PHP中JSON到多维数组和按键排序
我需要把它放在一个表格中,按照奖牌最多的国家进行排序,并输出计数和奖牌数PHP中JSON到多维数组和按键排序,php,multidimensional-array,Php,Multidimensional Array,我需要把它放在一个表格中,按照奖牌最多的国家进行排序,并输出计数和奖牌数 [ { "competitorName": "Usain Bolt", "country": "Jamaica", "event": "100m", "medalWon": "G", "worldRecord": "Y" }, { "competitorName": "Dave Batista",
[
{
"competitorName": "Usain Bolt",
"country": "Jamaica",
"event": "100m",
"medalWon": "G",
"worldRecord": "Y"
},
{
"competitorName": "Dave Batista",
"country": "United States",
"event": "Wrestling",
"medalWon": "G",
"worldRecord": "Y"
},
{
"competitorName": "Leonel Messi",
"country": "Argentina",
"event": "Soccer \/ Football",
"medalWon": "G",
"worldRecord": "N"
},
{
"competitorName": "Angel Di Maria",
"country": "Argentina",
"event": "Soccer \/ Football",
"medalWon": "G",
"worldRecord": "N"
}
]
$form=file\u get\u contents(“data.json”);
$data=json_decode($form,true);
这是我目前掌握的代码,数据来自json到多维数组。我不知道如何正确地构造它,以便我可以循环输入country name创建一个计数器变量,然后存储奖牌数量。您可以使用如下数组列(键)对数组进行排序
$form = file_get_contents("data.json");
$data = json_decode($form,true);
<section>
<div class="container">
<div class="row">
<div class="col col-md-6">
<ul><br>
<?php foreach($data as $country) {
echo "<li>". $country['country']. " has the following medal in " .$country['event']. "</li>";
echo "<li>".$country['competitorName']. "</li>";
echo "<li>".$country['medalWon']. "</li>";
foreach($country as $key => $value) {
}
echo "<br>";
}?>
</ul>
</div>
</div>
</div>
</section>
谢谢。我要找的是类似于在数组中循环,然后使用给定的国家名称创建一个数组。在该阵列中,我需要添加另一个包含奖牌数的阵列,一个包含奖牌数的列,另一个包含世界记录的列。例如,阿根廷出现了两次,但我希望重复的国家有一个数组,然后在该数组中,根据赢得的奖牌或竞争对手的名称和他们的奖牌,另一个数组。
<?php
$array = '[
{
"competitorName": "Usain Bolt",
"country": "Jamaica",
"event": "100m",
"medalWon": "G",
"worldRecord": "Y"
},
{
"competitorName": "Dave Batista",
"country": "United States",
"event": "Wrestling",
"medalWon": "G",
"worldRecord": "Y"
},
{
"competitorName": "Leonel Messi",
"country": "Argentina",
"event": "Soccer \/ Football",
"medalWon": "G",
"worldRecord": "N"
},
{
"competitorName": "Angel Di Maria",
"country": "Argentina",
"event": "Soccer \/ Football",
"medalWon": "G",
"worldRecord": "N"
}
]';
$json = json_decode($array,true);
$countries = array_column($json, 'country'); // Get countries in to another array
// Then sort according to the names in above countries array ASC
call_user_func_array('array_multisort', array($countries, SORT_ASC, SORT_STRING, &$json));
print_r($json);
?>