0

I'm trying to parse this json data into a php variable.

[{"type":"browsers","list":[{"key":"mobilesafari","value":3},{"key":"safari","value":1},{"key":"chrome","value":1}],"cardinality":3},{"type":"countries","list":[{"key":"US","value":3},{"key":"KR","value":1},{"key":"BR","value":1}],"cardinality":3},{"type":"languages","list":[{"key":"en-us","value":3},{"key":"pt-br","value":1},{"key":"ko-kr","value":1}],"cardinality":3},{"type":"organisations","list":[{"key":"Williams-Sonoma","value":1},{"key":"Verizon Wireless","value":1},{"key":"SK Telecom","value":1},{"key":"Oi Velox","value":1},{"key":"CenturyLink","value":1}],"cardinality":5},{"type":"platforms","list":[{"key":"iphone","value":2},{"key":"win8","value":1},{"key":"mac108","value":1},{"key":"ipad","value":1}],"cardinality":4}] 

Im trying to set it so i have a variable for broswers, countries etc.

Ive tried using

$foo = $json->browsers;

But I'm guessing that wont work because that isnt the name of the var.

Is there any non complex way of doing this?

Thanks

2
  • 3
    json_decode() Commented Jun 18, 2015 at 0:58
  • 1
    possible duplicate of Parsing JSON in PHP Commented Jun 18, 2015 at 1:16

3 Answers 3

0

As stated in the comments, you need to decode the json first, using json_decode().

$data = json_decode($json);

Now, for your data. It's an array of objects, which means you'll have to loop it:

foreach($data as $obj) {
    if($obj->type == 'browsers') {
        print_r($obj);
    }
}

Example

Sign up to request clarification or add additional context in comments.

Comments

0

Use json_decode() but pass in the a second parameter of true. Doing so will return an array rather than an object. You can then loop trough the array to grab each of the sets of data.

$json = '[{"type":"browsers","list":[{"key":"mobilesafari","value":3},{"key":"safari","value":1},{"key":"chrome","value":1}],"cardinality":3},{"type":"countries","list":[{"key":"US","value":3},{"key":"KR","value":1},{"key":"BR","value":1}],"cardinality":3},{"type":"languages","list":[{"key":"en-us","value":3},{"key":"pt-br","value":1},{"key":"ko-kr","value":1}],"cardinality":3},{"type":"organisations","list":[{"key":"Williams-Sonoma","value":1},{"key":"Verizon Wireless","value":1},{"key":"SK Telecom","value":1},{"key":"Oi Velox","value":1},{"key":"CenturyLink","value":1}],"cardinality":5},{"type":"platforms","list":[{"key":"iphone","value":2},{"key":"win8","value":1},{"key":"mac108","value":1},{"key":"ipad","value":1}],"cardinality":4}]';

foreach(json_decode($json,true) as $arrEachItem){
    switch($arrEachItem['type']){
        case'countries': $arrCountries = $arrEachItem['list']; break;
        case'browsers': $arrBrowsers = $arrEachItem['list']; break;
        case'languages': $arrLanguages = $arrEachItem['list']; break;
        case'organisations': $arrOrganisations = $arrEachItem['list']; break;
        case'platforms': $arrPlatforms = $arrEachItem['list']; break;
    }
}

//Output each array to the screen
echo "<pre>";
print_r($arrCountries);
print_r($arrBrowsers);
print_r($arrLanguages);
print_r($arrOrganisations);
print_r($arrPlatforms);

Comments

0
$data='[{"type":"browsers","list":[{"key":"mobilesafari","value":3},{"key":"safari","value":1},{"key":"chrome","value":1}],"cardinality":3},{"type":"countries","list":[{"key":"US","value":3},{"key":"KR","value":1},{"key":"BR","value":1}],"cardinality":3},{"type":"languages","list":[{"key":"en-us","value":3},{"key":"pt-br","value":1},{"key":"ko-kr","value":1}],"cardinality":3},{"type":"organisations","list":[{"key":"Williams-Sonoma","value":1},{"key":"Verizon Wireless","value":1},{"key":"SK Telecom","value":1},{"key":"Oi Velox","value":1},{"key":"CenturyLink","value":1}],"cardinality":5},{"type":"platforms","list":[{"key":"iphone","value":2},{"key":"win8","value":1},{"key":"mac108","value":1},{"key":"ipad","value":1}],"cardinality":4}]';

if( !function_exists('pre') ){/* utility function for formatted output - aids debugging */
    function pre( $data ){
        echo '<pre>',print_r( $data, 1 ),'</pre>';
    }
}

if( !function_exists('createobject') ){
    function createobject( $data=false ){
        try{
            if( !$data )throw new Exception('Bad value supplied for input data');

            /* decode the supplied data */
            $json=json_decode( $data );

            /* re-use the variable but reassign as an object */
            $data=new stdClass;

            /* Iterate through the json data and construct out output object */
            foreach( $json as $obj ){
                try{
                    /*
                        A temporary array to store each list item
                        ~ could potentially use `array_map` here
                    */
                    $tmp=new stdClass;
                    foreach( $obj->list as $i => $item ){
                        $tmp->{$item->key}=$item->value;
                    }

                    /* Assign new property to output object */
                    $data->{$obj->type}=$tmp;
                }catch( Exception $e ){
                    continue;
                }
            }
            return $data;
        }catch( Exception $e ){
            echo $e->getMessage();
        }
    }
}
/* To use new object */
$obj=createobject( $data );
pre( $obj );
pre( $obj->browsers );
pre( $obj->organisations );
echo $obj->languages->{'en-us'}; //3

This will output:

stdClass Object
(
    [browsers] => stdClass Object
        (
            [mobilesafari] => 3
            [safari] => 1
            [chrome] => 1
        )

    [countries] => stdClass Object
        (
            [US] => 3
            [KR] => 1
            [BR] => 1
        )

    [languages] => stdClass Object
        (
            [en-us] => 3
            [pt-br] => 1
            [ko-kr] => 1
        )

    [organisations] => stdClass Object
        (
            [Williams-Sonoma] => 1
            [Verizon Wireless] => 1
            [SK Telecom] => 1
            [Oi Velox] => 1
            [CenturyLink] => 1
        )

    [platforms] => stdClass Object
        (
            [iphone] => 2
            [win8] => 1
            [mac108] => 1
            [ipad] => 1
        )

)
stdClass Object
(
    [mobilesafari] => 3
    [safari] => 1
    [chrome] => 1
)
stdClass Object
(
    [Williams-Sonoma] => 1
    [Verizon Wireless] => 1
    [SK Telecom] => 1
    [Oi Velox] => 1
    [CenturyLink] => 1
)
3

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.