DomingoSL
DomingoSL

Reputation: 15494

Json parsing for general tree visualization of data on php

Recently i found a amazing javascript visualization library, and I want to integrate it to my actual webapp. Im fetching data from the facebook API in this format:

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
        )

)

From the data array im taking category and name, unfortunately the information is not classified by category, my idea if in the center of the tree I have the user name, then from this point each branch goes to one category, and from each category one branch from each name. Example. This is the way I need to arrange that, please take a look of the json var.

I can build that format just using php string functions like i did for other format i needed for the 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 "]);";

But since this new format is JSON (and im noob on json) im pretty sure there is a better way to build that new var I need using the json capabilities of php. The idea is to generated the json var in php and then I echo that of the JS.

Upvotes: 0

Views: 559

Answers (1)

Ascherer
Ascherer

Reputation: 8093

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

Upvotes: 1

Related Questions