来自URL的JqGrid数据

时间:2015-03-15 03:30:24

标签: php json jqgrid

我正在从API解码JSON:

$api = "http://xyzdomain.com/api";
$json = file_get_contents($api);
$array = json_decode($json,true);
print_r($array); 

以下是API对象和数组的示例结果。

Array
(
[status] => ok
[count] => 1
[meta] => Array
        (
            [count] => 1
        )

[data] => Array
        (
                [accountid] => Array
                (
                    [0] => Array
                        (
                            [all] => Array
                                (
                                    [fans] => 16
                                    [user post] => 333
                                    [user Details] => xyz
                                )
                            [scorelist] => 1
                            [name] => John Doe 1
                            [timespent] => 5887
                            [nation] => usa
                        )
                    [1] => Array
                        (
                            [all] => Array
                                (
                                    [fans] => 123
                                    [user post] => 903
                                    [user Details] => mno
                                )
                            [scorelist] => 6
                            [name] => John Doe 2
                            [timespent] => 1269
                            [nation] => usa
                        )
                    [2] => Array
                        (
                            [all] => Array
                                (
                                    [fans] => 16
                                    [user post] => 303
                                    [user Details] => abc
                                )
                            [scorelist] => 1
                            [name] => John Doe 3
                            [timespent] => 9292
                            [nation] => ussr
                        )
                    [3] => Array
                        (
                            [all] => Array
                                (
                                    [fans] => 16
                                    [user post] => 333
                                    [user Details] => jqr
                                )
                            [scorelist] => 1
                            [name] => John Doe 4
                            [timespent] => 75600
                            [nation] => usa
                        )
                )
        )
)

我上面只使用了三个样本,但最多可能达到500个样本。

我基本上有两个问题:

  1. 我想使用jqgrid或datatables或其他任何看起来很好的方式创建一个表。
  2. 在上面的例子中,我如何总结那个国家的时间,比如美国?

1 个答案:

答案 0 :(得分:0)

$timespent_usa = 0;
foreach($array['data']['accountid'] as $account) {

    if ($account['nation'] == 'usa') {
        $timespent_usa += $account['timespent'];
    }
}
echo $timespent_usa;

我不确定您计划如何将数据导入数据表,您可以将foreach循环中每个$account数组中的数据回显到<td>元素。