选择具有PHP条件的JSON数组

时间:2011-01-29 07:50:03

标签: php jquery

我有一个文本文件,可正确显示数据表中的所有记录,但我只想显示带有“ram”的记录。例如:

{ "aaData": [
["ram","india"],
["siva","india"],
["mani","india"],
["ram","india"],
["ram","india"]
] }

我正在使用PHP和Symfony1.3。请帮帮我。

2 个答案:

答案 0 :(得分:1)

你可以试试这个:

$array = json_decode($json_str, true); // convert json to array
print_r($array['ram']); // display items with ram key

答案 1 :(得分:1)

试试这个:

$json_str = '{ "aaData": [
["ram","india"],
["siva","india"],
["mani","india"],
["ram","india"],
["ram","india"]
] }
';
$array = json_decode($json_str, true); 
$entries_with_ram = array();
foreach($array['aaData']  as $entry)
    if($entry[0] == "ram")
        $entries_with_ram[] = $entry;

print_r($entries_with_ram);