Json数据组按值使用php

时间:2016-04-30 08:48:20

标签: php json function grouping

我希望显示基于池组的团队排名。

这是我的Json数据

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

当前用于输出代码的php是:

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";
}

我想根据池号对其进行分组。请在此处查看结果:

enter image description here

1 个答案:

答案 0 :(得分:1)

您需要将每一行分配到一个组数组,然后根据该行打印内容。

$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";
    }
}