Arasu
Arasu

Reputation: 2148

Selecting JSON array with conditions in PHP

I have a text file which displays all the records in datatables correctly but I want to display only the records with "ram". For example:

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

I am using PHP and Symfony1.3. Please help me.

Upvotes: 1

Views: 274

Answers (2)

Arshdeep
Arshdeep

Reputation: 4323

try this :

$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);

Upvotes: 1

Sarfraz
Sarfraz

Reputation: 382806

You could try this:

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

Upvotes: 1

Related Questions