Json解析php上的数据的一般树可视化

时间:2013-01-15 22:05:50

标签: php json visualization

最近我发现了一个令人惊叹的javascript可视化库,我想将它集成到我的实际webapp中。我以这种格式从facebook API获取数据:

Array
(
    [data] => Array
        (
            [0] => Array
                (
                    [category] => Community
                    [name] => Different Solutions
                    [id] => 271915499553486
                    [created_time] => 2012-09-23T17:17:23+0000
                )

            [1] => Array
                (
                    [category] => Non-profit organization
                    [name] => Indigeni Digitali
                    [id] => 295601322240
                    [created_time] => 2012-01-23T19:32:13+0000
                )

            [2] => Array
                (
                    [category] => Cause
                    [name] => StartupVisa
                    [id] => 10150111295350167
                    [created_time] => 2011-12-11T19:12:51+0000
                )

            [3] => Array
                (
                    [category] => Business services
                    [name] => H-FARM Ventures
                    [id] => 84021019572
                    [created_time] => 2011-04-02T02:30:26+0000
                )

            [4] => Array
                (
                    [category] => News/media website
                    [name] => iSpazio
                    [id] => 179295038271
                    [created_time] => 2010-05-28T21:12:28+0000
                )

            [5] => Array
                (
                    [category] => Website
                    [name] => Hot Pin Venezuela
                    [id] => 110628642307976
                    [created_time] => 2010-04-20T18:45:08+0000
                )

            [6] => Array
                (
                    [category] => Website
                    [name] => El Chiguire Bipolar
                    [id] => 14588159235
                    [created_time] => 2008-11-07T02:27:12+0000
                )

        )

    [paging] => Array
        (
            [next] => https://graph.facebook.com/1125405534/likes?limit=5000&offset=5000&__after_id=14588159235
        )

)

数据数组中取得类别名称,遗憾的是这些信息未按类别分类,我的想法是否在中心位置树我有用户名,然后从这一点开始,每个分支转到一个类别,每个分类从每个名称分支一个分支。 ExampleThis is the way我需要安排一下,请看一下 json var。

我可以使用php字符串函数构建该格式,就像我为google chart api所需的其他格式所做的那样:

    foreach($user_likes['data'] as $category) {
        if(isset($norm_interests[$category['category']])) { 
            $norm_interests[$category['category']] += 1; } 
        else { 
            $norm_interests[$category['category']] = 1; }
    }

    echo "data.addRows([";
    while ($category = current($norm_interests)) {
        if(key($norm_interests) <> 'Website') { 
        echo "['" . key($norm_interests) . "', " . $category . "],"; 
        }
        next($norm_interests);
    }
    echo "]);";

但是因为这个新格式是JSON(和json上的im noob)我非常确定有一种更好的方法来构建我需要使用php的json功能的新var。我的想法是在php中生成json var然后我回应JS的那个。

1 个答案:

答案 0 :(得分:1)

foreach($user_likes['data'] as $category) {
    if(isset($norm_interests[$category['category']])) { 
        $norm_interests[$category['category']] += 1; } 
    else { 
        $norm_interests[$category['category']] = 1; }
    }

    $data = json_encode( $norm_interests );
    echo "data.addRows(" . $data . ");";
}