tags:

views:

116

answers:

3

using the below code for decoding json

$categories = json_decode($data);
$categories = $categories->data;

where i get this

{"categories":[{"id":1,"name":"Utilities","apps":897,"iconurl":"http:\/\/static.apptrackr.org\/caticons\/uti.jpg"},{"id":2,"name":"Productivity","apps":477,"iconurl":"http:\/\/static.apptrackr.org\/caticons\/pro.jpg"},{"id":3,"name":"Music","apps":466,"iconurl":"http:\/\/static.apptrackr.org\/caticons\/mus.jpg"},{"id":4,"name":"Travel","apps":289,"iconurl":"http:\/\/static.apptrackr.org\/caticons\/tra.jpg"},{"id":5,"name":"Navigation","apps":297,"iconurl":"http:\/\/static.apptrackr.org\/caticons\/nav.jpg"},{"id":6,"name":"Books","apps":271,"iconurl":"http:\/\/static.apptrackr.org\/caticons\/boo.jpg"},{"id":7,"name":"Healthcare & Fitness","apps":250,"iconurl":"http:\/\/static.apptrackr.org\/caticons\/hea.jpg"},{"id":8,"name":"Games","apps":5116,"iconurl":"http:\/\/static.apptrackr.org\/caticons\/gam.jpg"},{"id":9,"name":"Social Networking","apps":272,"iconurl":"http:\/\/static.apptrackr.org\/caticons\/soc.jpg"},{"id":10,"name":"Lifestyle","apps":434,"iconurl":"http:\/\/static.apptrackr.org\/caticons\/lif.jpg"},{"id":11,"name":"Finance","apps":200,"iconurl":"http:\/\/static.apptrackr.org\/caticons\/fin.jpg"},{"id":12,"name":"News","apps":128,"iconurl":"http:\/\/static.apptrackr.org\/caticons\/new.jpg"},{"id":13,"name":"Photography","apps":481,"iconurl":"http:\/\/static.apptrackr.org\/caticons\/pho.jpg"},{"id":14,"name":"Entertainment","apps":1251,"iconurl":"http:\/\/static.apptrackr.org\/caticons\/ent.jpg"},{"id":15,"name":"Business","apps":221,"iconurl":"http:\/\/static.apptrackr.org\/caticons\/bus.jpg"},{"id":16,"name":"Sports","apps":199,"iconurl":"http:\/\/static.apptrackr.org\/caticons\/spo.jpg"},{"id":17,"name":"Education","apps":433,"iconurl":"http:\/\/static.apptrackr.org\/caticons\/edu.jpg"},{"id":18,"name":"Medical","apps":262,"iconurl":"http:\/\/static.apptrackr.org\/caticons\/med.jpg"},{"id":19,"name":"Weather","apps":64,"iconurl":"http:\/\/static.apptrackr.org\/caticons\/wea.jpg"},{"id":20,"name":"Reference","apps":419,"iconurl":"http:\/\/static.apptrackr.org\/caticons\/ref.jpg"}]} 

and i would like to convert to in an array like this

Array[0]
    {
       id => 1
       name => Utilities
       apps => 897
       iconurl => http:\/\/static.apptrackr.org\/caticons\/uti.jpg
    }

and so on

A: 

take a look at get_object_vars http://php.net/manual/en/function.get-object-vars.php

Vance
+5  A: 

This looks like a JSON string. You can use json_decode() to convert it into a PHP variable, e.g.

$obj = json_decode($json);
print_r($obj->categories); // array of StdClass objects

You can access and iterate the categories array regularly

echo $obj->categories[0]->name; // Utilities
echo $obj->categories[1]->name; // Productivity
echo $obj->categories[2]->name; // Music

To convert the StdClass objects to arrays, you could do

$categories = array();
foreach (json_decode($json)->categories as $category) {
    $categories[] = (array) $category;
}
print_r($categories);

You could also do it with a lambda function and array_map:

// Before PHP5.3
$categories = array_map(
    create_function('$el', 'return (array) $el;'), 
    json_decode($json)->categories);

// After PHP5.3
$categories = array_map(
    function($el) { return (array) $el; }, 
    json_decode($json)->categories);
Gordon
sorry for not declaring this but to get this i used $categories = json_decode($data);$categories = $categories->data;where i got the result aboveand when i try using your code i get error on foreachthanks for the help
Mahmoud
@Mahmoud There is no `$categories->data` in the object string you gave. What's the error you get?
Gordon
here is a link for a print_r(json_decode($categories)); http://pastie.org/844979 the one in my question is after i used $categories = $categories->data; the error i get in foreach ($arr->categories as $category) {
Mahmoud
@Mahmoud I'm not sure to understand what you are doing there, but will `json_decode($categories)` with `$categories` being derived from your `$categories = json_decode($data); $categories = $categories->data;` work? Apparently you have an object containing a JSON string in `data`.
Gordon
sorry my mistake i have updated my question where you can see and understand what i have done and here is the full code http://pastie.org/845028 where you can see and teach me what i am doing worng thanks again i appricate helping me
Mahmoud
Like I said, `data` contains a JSON string you will need to decode too to convert it to an object. See pasty.
Gordon
@Gordon, i love you man you have saved my life
Mahmoud
@Mahmoud you're welcome.
Gordon
A: 

@Gordon seems correct - that looks like JSON. Assuming, though, that you're dealing with an "actual" PHP Object, then it will be iterable; simply run through it with a foreach and push each key/value pair into your destination array.

Chris