This question already has an answer here:
How to extract and access data from JSON with PHP?
(1 answer)
Closed 1 year ago.
I have a url that returns a json. I want to loop through to get each of the values in the json.
This is what my json looks like.
{"error":false,
"message":"Request orders successfully completed",
"orders":[{
"oid":505,
"uid":234,
"total_amount":"143.99000"
},
{
"oid":506,
"uid":234,
"total_amount":"1.19000"
}]
}
My PHP code
$getOrdersUrl = file_get_contents("https://apiurl?last_id=504");
$json = json_decode($getOrdersUrl, true);
if(!empty($json)){
foreach($json as $val)
{
$oid = $val['orders'][0]['oid'];
$uid = $val['orders'][0]['uid'];
$total_amount = $val['total_amount'];
echo $oid;
}
}else{
echo "No Data to fetched to fetch for <br>";
}
<?php
$json = file_get_contents('https://api.jsonbin.io/b/6172d48d9548541c29c6ff05');
$data = json_decode($json, true);
foreach ($data['orders'] as $key => $value) {
echo "oid = ".$value['oid']."<br>";
echo "uid = ".$value['uid']."<br>";
echo "total_amount = ".$value['total_amount']."<br>";
}
?>
Related
This question already has answers here:
PHP foreach change original array values [duplicate]
(5 answers)
How to update/edit a JSON file using PHP [closed]
(1 answer)
Closed 2 years ago.
Please help me!
JSON:
{
"1":{
"TchID":"G303992",
"TchData":{
"TchID":"G303992",
"TchNama":"G303992",
"TchPassword":43511824
}
},
"2":{
"TchID":"G141843",
"TchData":{
"TchID":"G141843",
"TchNama":"G141843",
"TchPassword":22932450
}
}
}
How to update value in nested object from
"2":{
"TchID":"G141843",
"TchData":{
"TchID":"G141843",
"TchNama":"G141843",
"TchPassword":22932450
}
}
to
"2":{
"TchID":"G141843",
"TchData":{
"TchID":"G141843",
"TchNama":"Alex J",
"TchPassword":22932450
}
}
in php script?????
You see that i want to change "TchNama":"G141843" to "TchNama":"Alex J"
Here my code
<?php
$data = '{"1":{"TchID":"G303992","TchData":{"TchID":"G303992","TchNama":"G303992","TchPassword":43511824}},"2":{"TchID":"G141843","TchData":{"TchID":"G141843","TchNama":"G141843","TchPassword":22932450}}}';
$guru = json_decode($data);
foreach ($guru as $items){
if($items['TchID'] == 'G303992'){
// i dont know how to change $items['TchData']['TchNama'] = 'G141843' to 'Alex J'
$viewchange = json_encode($guru);
echo $viewchange;
}
}
?>
You can covert the json string to an array-object using json_encode($,TRUE).
Then you will be able to loop through the keys of the object if you obtain the keys by array_keys().
And then you can either use a separate variable to iterate through the main object properties and change that object, or directly access the main object and change it at the source, which is what I did below:
$data = '{"1":{"TchID":"G303992","TchData":{"TchID":"G303992","TchNama":"G303992","TchPassword":43511824}},
"2":{"TchID":"G141843","TchData":{"TchID":"G141843","TchNama":"G141843","TchPassword":22932450}}}';
$guru = json_decode($data, true);
$keys = array_keys($guru);
foreach ($keys as $key) {
if($guru[$key]['TchID'] == 'G303992'){
$guru[$key]["TchNama"] = "Alex J";
}
}
$viewchange = json_encode($guru);
echo $viewchange;
Please try this.
$data = '{"1":{"TchID":"G303992","TchData":{"TchID":"G303992","TchNama":"G303992","TchPassword":43511824}},"2":{"TchID":"G141843","TchData":{"TchID":"G141843","TchNama":"G141843","TchPassword":22932450}}}';
$guru = json_decode($data);
var_dump($guru);
foreach ($guru as $items) {
if ($items->TchID == 'G141843') {
$items->TchData->TchNama = "Alex J";
}
}
var_dump($guru);
$viewchange = json_encode($guru);
echo $viewchange;
This question already has an answer here:
How to extract and access data from JSON with PHP?
(1 answer)
Closed 3 years ago.
I want to get id,competitionId,competitionDisplayName of games in the JSON data.My current code doesn't output anything could any one tell me what i am doing wrong.Thanks
$json_string = 'https://webws.365scores.com/web/games/?langId=27&timezoneName=Asia/Hebron&userCountryId=-1&appTypeId=5&sports=1&startDate=17/08/2019&endDate=17/08/2019';
$json = file_get_contents($json_string);
$array = json_decode($json);
foreach($array as $values)
{
$output = $values->games['competitionDisplayName'] . "\n";
}
echo $output;
Try this:
<?php
$json_string = 'https://webws.365scores.com/web/games/?langId=27&timezoneName=Asia/Hebron&userCountryId=-1&appTypeId=5&sports=1&startDate=17/08/2019&endDate=17/08/2019';
$json = file_get_contents($json_string);
$result = json_decode($json);
foreach ($result->games as $game) {
$output .= $game->competitionDisplayName . "\n";
}
echo $output;
I checked your url, the data coming in for games is an array, so you need to loop through that to get the values of each game.
$array = json_decode($json);
foreach($array->games as $game){
$output = $game->competitionDisplayName;
prinr_r($output);
}
This question already has answers here:
How to convert a string to JSON object in PHP
(6 answers)
Closed 4 years ago.
I have created a Soap client to request data from web services and the returned data is displayed like this with my CodeIgniter function"
{"GetCodeResult":
{
"Selling":"1000.67114",
"Buying":"9000.65789"
}
}
but I want to format them like this by removing the getCodeResult
{"selling":"1000.67114","Buying":"9000.65789"}
I hope this may help you.
<?php
$jsonData = '{"GetCodeResult":
{
"Selling":"1000.67114",
"Buying":"9000.65789"
}
}';
$data = json_decode($jsonData, true);
$arr_index = array();
foreach ($data as $key => $value) {
$arr_index = $value;
}
echo json_encode($arr_index, true);
?>
Thanks.
If you want to get only the values of selling and buying means, try like below.
<?php
$jsonData = '{"GetCodeResult":
{
"Selling":"1000.67114",
"Buying":"9000.65789"
}
}';
$data = json_decode($jsonData, true);
$arr_index = array();
foreach ($data as $key => $value) {
if($key == 'GetCodeResult'){
$arr_index = $value;
}
}
foreach($arr_index as $values){
$finalVal[] = $values;
}
echo json_encode($finalVal, true);
?>
Decode to array and return only the 'GetCodeResult' then json_encode it.
echo json_encode(json_decode($json, true)['GetCodeResult']);
//{"Selling":"1000.67114","Buying":"9000.65789"}
https://3v4l.org/59RSc
To only echo the values you can assign them to variables using list and array_values.
list($selling, $buying) = array_values(json_decode($json, true)['GetCodeResult']);
echo $selling .PHP_EOL;
echo $buying .PHP_EOL;
//1000.67114
//9000.65789
https://3v4l.org/T5GNg
This question already has answers here:
Parsing JSON in PHP
(2 answers)
Closed 5 years ago.
I got a JSON from a web site in the raw format:
{"A_B":{"id":7,"last":"0.00000038"},"A_C":{"id":8,"last":"0.00001938"}, ... }
How do I get the A_B and A_C??? I do not know what A_B and A_C could be.
Try the following.
$json = file_get_contents("your json data");
$arr = json_decode($json, true);
foreach ($arr as $key=>$val) {
var_dump($key);
}
Finally, var_dump($key) displays A_B and A_C.
Suppose you have that raw json in some variable, say $yourJson
First, parse your json. $parsedJson = json_decode($yourJson, true)
Then run a foreach loop and stop after 2 iterations, You will (hopefully) get the right key-value pair.
$i = 0;
$extractedValues = [];
foreach ($parsedJson as $key => $value) {
$extractedValues[$key] = $value; $i++;
if ($i === 2) {
break;
}
}
Now, $extractedValues contains only two elements found from the first two iterations.
$data = json_decode("Your json variable");
foreach($data as $value){
echo $value; // here you receive your desire value.
}
Try this:
$json = '{"A_B":{"id":7,"last":"0.00000038"},"A_C":{"id":8,"last":"0.00001938"}}';
$dataArray = json_decode($json, true);
$arrayKeys = array_keys($dataArray); // in your case A_B and A_C
and if you want to get their values then:
foreach($dataArray as $data) {
foreach($data as $key => $value) {
echo $key . ": " . $value . PHP_EOL;
}
}
var $jsonObj = json_decode('{"A_B":{"id":7,"last":"0.00000038"}}')
print $jsonObj->{'A_B'}
This question already has an answer here:
How to extract and access data from JSON with PHP?
(1 answer)
Closed 5 years ago.
I have json data like this:
{"result":{"total":19,"hits":[{"keyword":"sidney webb"},{"keyword":"dr webb"}]},"status_msg":"OK","status_code":200,"left_lines":1196851}
I need to extract from json only keywords. I try without success with code:
$json = '{"result":{"total":19,"hits":[{"keyword":"sidney webb"},{"keyword":"dr webb"}]},"status_msg":"OK","status_code":200,"left_lines":1196851}';
foreach ($json['result'] as $keywords)
{
echo $keywords['result']['keyword'];
}
Seems, you forgot to decode the string with json_decode. In addition, your likely would to loop over [result][hits] and not just [result]
$json = '{"result":{"total":19,"hits":[{"keyword":"sidney webb"},{"keyword":"dr webb"}]},"status_msg":"OK","status_code":200,"left_lines":1196851}';
$json = json_decode($json, true);
foreach ($json['result']['hits'] as $hit)
{
echo $hit['keyword'];
}
TRY
$json = '{"result":{"total":19,"hits":[{"keyword":"sidney webb"},{"keyword":"dr webb"}]},"status_msg":"OK","status_code":200,"left_lines":1196851}';
$jsonp =json_decode($json,TRUE);
foreach ($jsonp['result'] as $val)
{
if(is_array($val) ){
foreach ($val as $k)
{
echo $k['keyword'];
}
}
}