Reputation: 79
I'm passing stringify JSON like this to my php file,
[{"Country Code":"bob","Country":"503","Description":"bobby","\"Minute Rate":"oregon","USD\"":"","\"$5 Talk Time":"\r"},{"Country Code":"steve","Country":"707","Description":"stevie","\"Minute Rate":"california","USD\"":"","\"$5 Talk Time":"\r"},{"Country Code":"dsfd","Country":"342","Description":"dfdfs","\"Minute Rate":"dfdsfs","USD\"":"","\"$5 Talk Time":"\r"},{"Country Code":"sada","Country":"342","Description":"sdsad","\"Minute Rate":"dfsffd","USD\"":"","\"$5 Talk Time":"\r"},{"Country Code":""}]
How can i run a loop to fetch these results in php?
Thank you!
Upvotes: 1
Views: 112
Reputation: 5444
Try this..
$json='[{"Country Code":"bob","Country":"503","Description":"bobby","\"Minute Rate":"oregon","USD\"":"","\"$5 Talk Time":"\r"},{"Country Code":"steve","Country":"707","Description":"stevie","\"Minute Rate":"california","USD\"":"","\"$5 Talk Time":"\r"},{"Country Code":"dsfd","Country":"342","Description":"dfdfs","\"Minute Rate":"dfdsfs","USD\"":"","\"$5 Talk Time":"\r"},{"Country Code":"sada","Country":"342","Description":"sdsad","\"Minute Rate":"dfsffd","USD\"":"","\"$5 Talk Time":"\r"},{"Country Code":""}]';
$result=json_decode($json, true);
foreach($result as $value)
{
echo $value['Country Code'];
}
Upvotes: 1
Reputation: 53
I think you should take a look at json_decode()
<?php
$jsonObject = json_decode($yourstring);
echo $jsonObject->{'json-key'};
?>
Upvotes: 0
Reputation: 3960
json_decode function takes a json string for its first parameter and an optional boolean (true/false) for its second parameter. The second parameter, if set to true, returns the json string as an associative array, if it’s not set it will return an object.
json_array = json_decode($json, true); //Converts to array
foreach($json_array as $json){
echo $json['key']; //Key
echo $json->key; //Value of key
}
FYI
Upvotes: 2
Reputation: 189
Try
json_decode(string, true);
Result will be an array and you can loop it.
Upvotes: 1