2010-02-26 73 views
3

使用用於解碼JSON下面的代碼如何將JSON字符串轉換爲數組(PHP)?

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

其中i得到這個

{"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"}]} 

,我想轉換成在一個陣列這樣

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

回答

5

這看起來像是JSON string。您可以使用json_decode()將其轉換爲PHP變量,例如

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

您可以訪問和迭代的類別陣列定期

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

到了一個StdClass對象轉換爲數組,你可以做

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

你也可以用lambda function並做到這一點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); 
+0

抱歉沒有宣佈這一點,但得到這個我用 $類= json_decode($數據); $ categories = $ categories-> data; 在那裏我得到上述 的結果,當我嘗試用你的代碼,我得到錯誤的的foreach 感謝您的幫助 – Mahmoud 2010-02-26 22:33:06

+0

@Mahmoud沒有'$分類 - > data'對象字符串中你給。你得到的錯誤是什麼? – Gordon 2010-02-26 22:37:33

+0

這裏是一個print_r的鏈接(json_decode($ categories)); http://pastie.org/844979在我的問題之一是我使用$ categories = $ categories-> data;我在foreach中得到的錯誤($ arr-> categories as $ category){ – Mahmoud 2010-02-26 22:58:15

0

@戈登似乎是正確的 - 那看起來像JSON。假設你正在處理一個「實際的」PHP對象,那麼它將是可迭代的;只需用foreach運行它並將每個鍵/值對推入目標陣列。

1

呃,你可以設置第二個參數,以JSON轉換成一個數組,而不是插入的對象:

$categories = json_decode($data, true);