Json data Group by value using php

2019-09-11 01:26发布

问题:

I want to display team ranking be based on pool group.

Here is my Json Data

    {"rows":[{"team":{"id":172725,"pool":1},"stats":[{"name":"played","value":1},{"name":"won","value":1},{"name":"lost","value":0},{"name":"points","value":"2.0"},{"name":"net_run_rate","value":"+6.325"}],"rank":"1"},
{"team":{"id":172726,"pool":3},"stats":[{"name":"played","value":0},{"name":"won","value":0},{"name":"lost","value":0},{"name":"points","value":"0.0"},{"name":"net_run_rate","value":"+0.000"}],"rank":"*"},
{"team":{"id":172727,"pool":2},"stats":[{"name":"played","value":1},{"name":"won","value":1},{"name":"lost","value":0},{"name":"points","value":"1.0"},{"name":"net_run_rate","value":"+4.455"}],"rank":"1"},
{"team":{"id":172728,"pool":1},"stats":[{"name":"played","value":1},{"name":"won","value":0},{"name":"lost","value":1},{"name":"points","value":"0.0"},{"name":"net_run_rate","value":"-6.325"}],"rank":"2"},
{"team":{"id":172729,"pool":2},"stats":[{"name":"played","value":1},{"name":"won","value":0},{"name":"lost","value":1},{"name":"points","value":"0.0"},{"name":"net_run_rate","value":"-4.455"}],"rank":"2"}]}

Current php to code to output is :

foreach ($read_json->rows as $item) {               
$id = empty($item->team->id) ? "" : $item->team->id;                
$pool  = empty($item->team->pool) ? "" : $item->team->pool;
$rank = empty($item->rank) ? "" : $item->rank;  
$played = empty($item->stats[0]->value) ? "0" : $item->stats[0]->value;
$won = empty($item->stats[1]->value) ? "0" : $item->stats[1]->value;
$lost = empty($item->stats[2]->value) ? "0" : $item->stats[2]->value;               
$points = empty($item->stats[9]->value) ? "0" : $item->stats[9]->value;
$net_run_rate= empty($item->stats[12]->value) ? "0" : $item->stats[12]->value;                      
echo "<div>$id</div><div>$rank</div><div>$played</div><div>$won</div><div>$lost</div><div>$points</div><div>$net_run_rate</div>\n";
echo " </div>\n";
}

I want to group it as per the pool number. Please see the result here:

回答1:

You'll want to assign each row to a group array and then print content based on that.

$pools = [];

foreach ($read_json->rows as $item)
{
    // Define this sub-array if it does not exist.
    if (!isset($pools[$item->team->pool]))
    {
        $pools[$item->team->pool] = [];
    }

    // Add this item to this pool's array.
    $pools[$item->team->pool][] = $item;
}

// Sort by key.
ksort($pools);

foreach ($pools as $pool_id => $pool)
{
    echo "<h1>Pool $pool_id</h1>";

    foreach ($pool as $item)
    {
        $id = empty($item->team->id) ? "" : $item->team->id;
        $pool  = empty($item->team->pool) ? "" : $item->team->pool;
        $rank = empty($item->rank) ? "" : $item->rank;  
        $played = empty($item->stats[0]->value) ? "0" : $item->stats[0]->value;
        $won = empty($item->stats[1]->value) ? "0" : $item->stats[1]->value;
        $lost = empty($item->stats[2]->value) ? "0" : $item->stats[2]->value;
        $points = empty($item->stats[9]->value) ? "0" : $item->stats[9]->value;
        $net_run_rate= empty($item->stats[12]->value) ? "0" : $item->stats[12]->value;

        echo "<div>$id</div><div>$rank</div><div>$played</div><div>$won</div><div>$lost</div><div>$points</div><div>$net_run_rate</div>\n";
    }
}