简体   繁体   中英

JSON to multidimensional array in PHP and sorting by key

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

I need to put this in a table and sort by the country with most medals and output the count and the medals.

$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>

This is the code that i have so far data is coming from json to a multidimensional array. I can't figure out how to structure it properly so I can loop for country name create a counter variable and then store the number of medals.

You can sort array using an array column (key) like this,

    <?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);
?>

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM