0

最近我发现了一个很棒的 javascript 可视化库,我想将它集成到我的实际 web 应用程序中。我以这种格式从 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
        )

)

从我获取类别名称的数据数组中,不幸的是,信息没有按类别分类,我的想法是如果在树的中心我有用户名,那么从这一点开始,每个分支都进入一个类别,每个类别都有一个从每个名称分支。例子这是我需要安排的方式,请看一下json var。

我可以使用 php 字符串函数来构建该格式,就像我为谷歌图表 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 上的菜鸟),我很确定有一种更好的方法可以使用 php 的 json 功能来构建我需要的新 var。这个想法是在 php 中生成 json var,然后我回应 JS 的那个。

4

1 回答 1

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 . ");";
}
于 2013-01-15T22:11:50.053 回答