2012-07-11 41 views
0
類別

我這些JSON數據:的Json解碼演示隨着

[{"category":"Pizza","name":"Beef Pronto","desc":"Description of Beef Pronton here","price":"12"}, 
{"category":"Drink","name":"Cool Delight","desc":"Description of Coold Delight here","price":"5"}, 
{"category":"Drink","name":"Cola","desc":"Description of Cola","price":"4"} 
] 

用JavaScript我已經成功地管理的數據呈現如下:

比薩

-Beef普羅託:說明牛肉Pronton here:12

飲料

酷斃了喜悅:5

-cola:可口可樂的說明:4

任何想法如何與PHP做Coold喜悅在這裏的描述?

- >好球員,這是我如何用PHP做:

<? 
$listedmenuJSON = '[{"category":"Pizza","name":"Beef Pronto","desc":"Description of Beef Pronton here","price":"12"}, 
{"category":"Drink","name":"Cool Delight","desc":"Description of Coold Delight here","price":"5"}, 
{"category":"Drink","name":"Cola","desc":"Description of Cola","price":"4"} 
]'; 

    $json_decoded = json_decode($listedmenuJSON); 
    foreach ($json_decoded as $categoryvalue){ 
    //echo $categoryvalue->category."<br/>"; 
    $tempcategoryvalue[] = $categoryvalue->category; 
    $arrunique = array_unique($tempcategoryvalue); 
    } 

    foreach ($arrunique as $tmpcategory){ 
    echo '<br/><b>'.$tmpcategory.'</b></br>'; 
    foreach ($json_decoded as $tempo){ 
     if($tempo->category == $tmpcategory){ 
     echo $tempo->name.'<br/>'; 
     echo '<i>'.$tempo->desc.'.......</i>'; 
     echo $tempo->price.'<br/>'; 
     } 
    } 

    }  
?> 

它會產生如下:

比薩
牛肉彈指一揮間
這裏的牛肉Pronton的描述....... 12

飲料
酷喜悅
說明Coold喜悅在這裏....... 5
可口可樂
可口可樂的描述....... 4

回答

0

如果您在PHP代碼中使用json使用以下行將json轉換爲PHP數組並操作數組以獲得期望輸出

$array = json_decode($json,TRUE); 

在JavaScript用戶下面的代碼將您的json轉換爲數組。

var obj = jQuery.parseJSON(responce); 
+0

這是相當有幫助的......至少我知道從哪裏開始,現在 – Gregory 2012-07-12 01:25:24

0

嘗試使用PHP函數'json_decode'(http://php.net/manual/en/function.json-decode.php)。這將在PHP中創建一個關聯數組。訪問和處理數據應該非常簡單。

+0

是的,我靠近它..但仍然試圖將我的Jscript邏輯轉換爲PHP,這不是我最強的一點 – Gregory 2012-07-12 01:21:50

0

如果使用SQL Server,那麼你可以選擇使用「按類別組」

else形式的表,你可以驗證多次的陣列。

SQL:

<?php 
$query = "SELECT * FROM 'table' 
GROUP BY category 
having (category=\"Pizza\");" 


--SELECT * FROM 'table' 
--GROUP BY category 
--having (category="Drink"); 

如果你使用這個在PHP中,你必須使用sql_query(「查詢」)

+0

不,我不用Db做任何事情..我已經得到了這個json – Gregory 2012-07-12 01:22:44