How do I get one json value value each? - php

in this my php code
echo $_POST[result];
in this my php result
{
"result_code":0,
"err_cd":"",
"result_msg":"",
"store_id":"M20C2685",
"status":"APPROVED",
"order_no":"600a2a044c9be",
"tr_no":725,
"tr_price":1000,
"pay_price":1000,
"approved_day":"20210122",
"approved_time":"102744",
"param1":"",
"param2":""
}
I want to print out each value one by one. What should I do?

json_decode Parsing JSON
<?php
$str = '{"result_code":0,
"err_cd":"",
"result_msg":"",
"store_id":"M20C2685",
"status":"APPROVED",
"order_no":"600a2a044c9be",
"tr_no":725,
"tr_price":1000,
"pay_price":1000,
"approved_day":"20210122",
"approved_time":"102744",
"param1":"",
"param2":""
}';
$arr = json_decode($str, true);
echo $arr['err_cd'] . PHP_EOL;
echo $arr['result_msg'] . PHP_EOL;
echo $arr['store_id'] . PHP_EOL;
echo $arr['status'] . PHP_EOL;
// ......
// Circulates each value in the array
foreach ($arr as $item) {
echo $item . PHP_EOL;
}

Related

PHP Best Way to Create Variable from API Content

I am very beginner in programmer and I am learning.
I am sorry if my question is too bad.
I want to create variable in php from api content, for example:
This contents is from this URL: http://example.com/api
{"name":"John","age":"20","genre":"male","language":[{"id":"22","name":"english"},{"id":"23","name":"french"}]}
<?php
$content = file_get_contents("http://example.com/api");
$content = str_replace('"', "", $content);
$content = str_replace(":", "=", $content);
$content = str_replace(",", "&", $content);
parse_str($content);
echo $name; //John
echo $age; //20
echo $genre; //male
echo $language //[{id <======== here is my problem
?>
My problem is when I am getting an array like "language", how to fix it?
Thanks for help.
U can use the http://www.php.net/json_decode in two ways :
This is object oriented :
$str = '{"name":"John","age":"20","genre":"male","language":[{"id":"22","name":"english"},{"id":"23","name":"french"}]}';
$json = json_decode($str);
echo 'name: ' . $json->{'name'} .'<br>';
echo 'age: ' . $json->{'age'} .'<br>';
echo 'genre: ' . $json->{'genre'} . '<br>';
foreach($json->{'language'} as $data){
echo 'id: ' . $data->{'id'} . '<br>';
echo 'name: ' . $data->{'name'} . '<br>';
}
As an associative array :
$json = json_decode($str, true);
echo 'name: ' . $json['name'] .'<br>';
echo 'age: ' . $json['age'] .'<br>';
echo 'genre: ' . $json['genre'] . '<br>';
foreach($json['language'] as $data){
echo 'id: ' . $data['id'] . '<br>';
echo 'name: ' . $data['name'] . '<br>';
}
json_decode() will help you to convert the string data to something more accessible:
<?php
// Instead of your fetched data we use static example data in this script
//$content = file_get_contents("http://example.com/api");
$content = '{"name":"John","age":"20","genre":"male","language":[{"id":"22","name":"english"},{"id":"23","name":"french"}]}';
// Convert json data to object
$data = json_decode($content);
// access object properties by using "->" operator
echo $data->name;
echo $data->age;
echo $data->genre;
// language is an array of objects, so let's look at each language object...
foreach($data->language as $lang) {
// ... and extract data using "->" again
echo $lang->id;
echo $lang->name;
}
A live example of this code can be found at http://sandbox.onlinephpfunctions.com/code/6df679c3faa8fff43308a34fb80b2eeb0ccfe47c
As pointed out by #fusionK, the response from the api request is a json string so convert to an object ( or array if preferred ) using json_decode ( json_decode( $data,true ) for an array )
Once it is decoded it is straightforward to access the properties of the object.
<?php
/* capture and decode response from api - creates an object */
$content = json_decode( file_get_contents("http://example.com/api") );
/* using object notation to access properties */
echo $content->name.' '.$content->age.' '.$content->genre;
/* for the language which is an array of objects */
$lang=$content->language;
foreach( $lang as $language ){
$obj=(object)$language;
echo $obj->id.' '.$obj->name;
}
?>

Loop JSON String response in PHP Foreach

I'm trying to print values of a JSON response in PHP. Inside the contents are:
data
local
acao
detalhes
Codes:
<?php
header('Content-type: text/html; charset=UTF-8');
$url = "http://developers.agenciaideias.com.br/correios/rastreamento/json/RJ290474594CN";
$c_url = file_get_contents($url);
$c_url = utf8_encode($c_url);
$results = json_decode($c_url);
echo $c_url;
?>
I've tried using foreach but wasn't successful in implementing them in said JSON response.
Here is a JSON String Example:
[{"data":"07\/12\/2014 11:19","local":"CHINA - CHINA\/CN","acao":"postado","detalhes":"-"},{"data":"09\/12\/2014 12:03","local":"CHINA - CHINA\/CN","acao":"encaminhado","detalhes":"Em tr\u00e2nsito para Unidade de Tratamento Internacional - BRASIL\/BR"},{"data":"29\/12\/2014 12:34","local":"UNIDADE TRAT INTERNACIONAL PARANA - Curitiba\/PR","acao":"conferido","detalhes":"Recebido\/Brasil "}]
If you just want to print values just print them accordingly using foreach. After getting the response, use json_decode(). That ut8f_encode() is superfluous.
$url = "http://developers.agenciaideias.com.br/correios/rastreamento/json/RJ290474594CN";
$data = json_decode(file_get_contents($url), true);
foreach($data as $values) {
echo $values['data'] . '<br/>';
echo $values['local'] . '<br/>';
echo $values['acao'] . '<br/>';
echo $values['detalhes'] . '<br/>';
}
Sample Output
Or:
$url = "http://developers.agenciaideias.com.br/correios/rastreamento/json/RJ290474594CN";
$data = json_decode(file_get_contents($url), true);
foreach($data as $values) {
foreach($values as $key => $value) {
echo "{$key}: $value <br/>";
}
echo '<hr/>';
}

PHP get json data with json_decode

I'm new to PHP and try to echo out some data from json, but I got stucked in this.
It shows non of the data, but the data is there. The var_dump() shows it to me.
Probably I don't use the array correctly, but I can't find what's wrong. I've this code,
I request some data which I gathered with knockout.js
Knockout gives me a json (as show below)
$json = $_REQUEST[seats];
echo 'requested data raw '. "<br>".$json;
$data = json_decode($json, true);
echo "<br>".'var_dump '. "<br>";
var_dump($data);
foreach ($data as $optie ) {
echo "name = " . $optie->name . "<br>";
echo "optie = " . $optie->optieName . "<br>";
echo "prijs = " . $optie->prijs . "<br>";
}
This is my JSON:
[
{
"name": "Naam 1",
"optie": {
"optieName": "Make_up",
"prijs": 9.95
},
"PrijsFormated": "Euro: 9.95"
},
{
"name": "Naam 2",
"optie": {
"optieName": "Handverzorging",
"prijs": 12.95
},
"PrijsFormated": "Euro: 12.95"
}
]
You should use such loop:
foreach ($data as $optie ) {
echo "name = " . $optie['name'] . "<br>";
echo "optie = " . $optie['optie']['optieName'] . "<br>";
echo "prijs = " . $optie['optie']['prijs']. "<br>";
}
Because using json_decode() with second parameter as true you have created associative array - documentation.
If you would like to access data as object, you should use:
$data = json_decode($json);
instead of
$data = json_decode($json, true);
and then you should use the following loop:
foreach ($data as $optie ) {
echo "name = " . $optie->name . "<br>";
echo "optie = " . $optie->optie->optieName . "<br>";
echo "prijs = " . $optie->optie->prijs. "<br>";
}

PHP foreach - parsed json from URL

I have a JSON file called from an URL. I've checked and I'm getting the the data from the URL.
I've tried a lot, but I can't get the loop foreach to work - what is wrong?
<?php
$url = 'http://banen.klintmx.dk/json/ba-simple-proxy.php?url=api.autoit.dk/car/GetCarsExtended/59efc61e-ceb2-463b-af39-80348d771999';
$json= file_get_contents($url);
$data = json_decode($json);
$rows = $data->{'contents'};
foreach($rows as $row) {
echo '<p>';
$FabrikatNavn = $row->{'contents'}->{'FabrikatNavn'};
$ModelNavn = $row->{'contents'}->{'ModelNavn'};
$PrisDetailDkk = $row->{'contents'}->{'PrisDetailDkk'};
echo $FabrikatNavn . $ModelNavn . ' Pris ' . $PrisDetailDkk;
echo '</p>';
}
?>
The actual problem is you trying to access content object again. Just change your foreach snippet with,
foreach ($rows as $row) {
echo '<p>';
$FabrikatNavn = $row->FabrikatNavn;
$ModelNavn = $row->ModelNavn;
$PrisDetailDkk = $row->PrisDetailDkk;
echo $FabrikatNavn . $ModelNavn . ' Pris ' . $PrisDetailDkk;
echo '</p>';
}
DEMO.
Use json_decode($data, true) so that it parses the JSON content into a PHP array. So it will be something like
$rows = $data['contents'];
foreach($rows as $row) {
echo '<p>';
$FabrikatNavn = $row['contents']['FabrikatNavn'];
$ModelNavn = $row['contents']['ModelNavn'];
$PrisDetailDkk = $row['contents']['PrisDetailDkk'];
echo $FabrikatNavn . $ModelNavn . ' Pris ' . $PrisDetailDkk;
echo '</p>';
}
Take a look at using json_decode($json, true) as this will convert the data to an associative array which seems to be the way you are approaching the solution.
Check the output by printing with var_dump() or print_r()
Try like this
$data = json_decode($json,true); //decode json result as array and thenloop it
print '<pre>';
print_r($data);
foreach($data as $row){
//do something here
}

PHP (XML TO ARRAY) Getting the attribute of the xml that is array

Hello I am getting games in the spilgames xml. I managed to convert it to array but 1 problem came up again the problem is that the string I want to get is inside of an array or an attribute of 1 node xml .I can't get the attribute.
I want to get the attribute.
website of spilgames: here
Code I have so far:
<?php
$xml = simplexml_load_string(file_get_contents('http://publishers.spilgames.com/rss-3?limit=100&format=xml&category=Action')); /* Get the xml */
$json = json_encode($xml);
$games = json_decode($json); /* Make it an array */
$game = $games->entries->entry;
foreach($game as $entry) { /* Each game information */
echo $entry->title;
echo $entry->id;
echo $entry->description;
echo $entry->category;
echo $entry->subcategory;
echo $entry->technology;
echo $entry->player->url;;
echo $entry->thumbnails->small->url; /* Problems starts here */
/* Because the thumbnails has 3 child but the info is inside of each child */
}
?>
I am getting the xml not json in spilgames because I don't know how json works.
If I were you I would learn how JSON works because it will make your life easier:
<?php
$json = file_get_contents('http://publishers.spilgames.com/rss-3?limit=100&format=json&category=Action');
$data = json_decode($json);
foreach ($data->entries as $entry) {
echo $entry->title . "\n";
echo $entry->id . "\n";
echo $entry->description . "\n";
echo $entry->category . "\n";
echo $entry->subcategory . "\n";
echo $entry->technology . "\n";
echo $entry->thumbnails->small . "\n";
echo $entry->thumbnails->medium . "\n";
echo $entry->thumbnails->large . "\n";
}
?>

Categories