i have the following JSON:
{"Switches":["Auswahl1","Auswahl2","Auswahl3"],"Check_MK":["Auswahl1","Auswahl2","Auswahl3"],"Testgroup":["Auswahl1","Auswahl2","Auswahl3"],"Printer":["Auswahl1","Auswahl2","Auswahl3"],"CAD":["Auswahl1","Auswahl2","Auswahl3"]}
How do i loop each object while using PHP?
My thoughts were the following:
<?php
$jsonfile = file_get_contents('tags.json');
echo $jsonfile . "<br><br>";
$decode = json_decode($jsonfile);
foreach($decode as $key => $value) {
echo $key . $value;
}
?>
Doesn't work..... Also
echo $decode[1];
and
echo $decode[1][1];
doesn't work..
You need to add a second parameter to json_decode()
This parameter returns associative array instead of existing object (if existing).
$decode = json_decode($jsonfile, TRUE);
This will convert your JSON decoded data into associative array.
$jsonfile = file_get_contents('tags.json');
echo $jsonfile . "<br><br>";
$decode = json_decode($jsonfile);
now $decode is equivalent to:
$decode = new stdClass();
$decode->Switches = array();
$decode->Switches[] = "Auswahl1";
$decode->Switches[] = "Auswahl2";
$decode->Switches[] = "Auswahl3";
$decode->Check_MK = array();
...
Related
I'm using iTunes RSS generator to get HOT tracks, now I'm using following way to parse JSON:
<?php
$json_string = 'https://rss.itunes.apple.com/api/v1/in/apple-music/hot-tracks/all/10/explicit.json';
$jsondata = file_get_contents($json_string);
$obj = json_decode($jsondata,true);
$cltn = $obj['feed']['results'][0]['collectionName'];
echo $cltn;
?>
Now, as we know that It'll return only 1 collectionName.
JSON request is returning 10 results. How can I get them all using foreach loop? I've used several ways but no success.
Since you didn't give the output of the array I assume the [0] index is what needs to be iterated.
You need to foreach the $obj['feed']['results'] by doing:
Foreach($obj['feed']['results'] as $cltn){
Echo $cltn['collectionName'];
}
Foreach over the results:
foreach ($obj['feed']['results'] as $result) {
echo $result['collectionName'] . '<br>' . PHP_EOL;
}
Based on the code you provided you can iterate the results to get all possible information from the artist/track list, for example:
$json_string = 'https://rss.itunes.apple.com/api/v1/in/apple-music/hot-tracks/all/10/explicit.json';
$jsondata = file_get_contents($json_string);
$obj = json_decode($jsondata,true);
$cltn = $obj['feed']['results'];
function test_print($item, $key)
{
echo "<strong>".$key."</strong>: ".$item."<br>";
}
foreach($cltn as $key => $c) {
echo "Result No ".($key+1)."<br>";
array_walk_recursive($c, 'test_print');
}
in case you only want to show artistName and collectionName you can slightly modify the above example:
$json_string = 'https://rss.itunes.apple.com/api/v1/in/apple-music/hot-tracks/all/10/explicit.json';
$jsondata = file_get_contents($json_string);
$obj = json_decode($jsondata,true);
$cltn = $obj['feed']['results'];
foreach($cltn as $c) {
echo $c['artistName'].": ".$c['collectionName']."<br>";
}
You can try all the above in PHP Fiddle
Try like this way with foreach() to iterate your array in key=>value manner as you've decoded the json as an array not an object in php.
<?php
$json_string = 'https://rss.itunes.apple.com/api/v1/in/apple-music/hot-tracks/all/10/explicit.json';
$jsondata = file_get_contents($json_string);
$array = json_decode($jsondata,true);
# printing resulted array just for debugging purpose
print '<pre>';
print_r($array);
print '</pre>';
foreach($array['feed']['results'] as $key=>$value){
echo $value['collectionName'].'<br/>';
}
?>
{
USD_INR: {
2017-12-31: 63.830002
},
INR_USD: {
2017-12-31: 0.015667
}
}
I have tried with following method,
$url = file_get_contents("https://free.currencyconverterapi.com/api/v5/convert?q=USD_INR,INR_USD&compact=ultra&date=2017-12-31");
$result = json_decode($url);
echo $result->USD_INR;
but it did't works, can any one help me where I'm doing wrong.
Just pass date in $todaysDate variable which you want price.
$todaysDate = "2017-12-31";
$url = file_get_contents("https://free.currencyconverterapi.com/api/v5/convert?q=USD_INR,INR_USD&compact=ultra&date=$todaysDate");
$result = json_decode($url);
echo ($result->USD_INR->$todaysDate);
You can add true as a second parameter on json_decode to make it associative array.
$url = file_get_contents("https://free.currencyconverterapi.com/api/v5/convert?q=USD_INR,INR_USD&compact=ultra&date=2017-12-31");
$result = json_decode($url, true);
And access it as:
echo $result["USD_INR"]["2017-12-31"];
This will result to: 63.830002
Doc: http://php.net/manual/en/function.json-decode.php
You can foreach loop to get all the values
$url = file_get_contents("https://free.currencyconverterapi.com/api/v5/convert?q=USD_INR,INR_USD&compact=ultra&date=2017-12-31");
$result = json_decode($url, true);
foreach ( $result as $value ) {
foreach ( $value as $date => $rate ) {
echo $date . ": " . $rate . "<br />";
}
}
This will result to:
2017-12-31: 63.830002
2017-12-31: 0.015667
You are trying to echo an object either use print_r like this to print USD_INR
$url = '{"USD_INR":{"2017-12-31":63.830002},"INR_USD":{"2017-12-31":0.015667}}';
$result = json_decode($url);
print_r($result->USD_INR);
OR to echo value of 2017-12-31 inside USD_INR
$url = '{"USD_INR":{"2017-12-31":63.830002},"INR_USD":{"2017-12-31":0.015667}}';
$result = json_decode($url);
echo $result->USD_INR->{'2017-12-31'};
Using {'2017-12-31'} because 2017-12-31 is not proper variable name
Live demo
I want to echo out each element of an object of a JSON array like this:
{"request_list":[{"id":"1","name":"yunus","surname":"smsk","phone_number":"05350601922","blood_type":"0","unit_of_blood":"0","date":null},{"id":"3","name":"yunus","surname":"smsk","phone_number":"05350601922","blood_type":"0","unit_of_blood":"0","date":null}]}
But i cannot do it.I tried somethinglikethis:
$object = json_decode($json, true);
$request_list = $object->request_list;
foreach($request_list as $r){
echo $r->name;
echo $r->blood_type;
echo $r->phone_number;
}
But I got an error like:
Invalid argument supplied for foreach()
As you have mark return as array true in json_decode. So, try below code.
$object = json_decode($json, true);
$request_list = $object['request_list'];
foreach($request_list as $r){
echo $r['name'];
echo $r['blood_type'];
echo $r['phone_number'];
}
Use this
$object = json_decode($json, true);
$request_list = $object['request_list'];
foreach($request_list as $r){
echo $r['name'];
echo $r['blood_type'];
echo $r['phone_number'];
}
try
$json = '{"request_list":[{"id":"1","name":"yunus","surname":"smsk","phone_number":"05350601922","blood_type":"0","unit_of_blood":"0","date":null},{"id":"3","name":"yunus","surname":"smsk","phone_number":"05350601922","blood_type":"0","unit_of_blood":"0","date":null}]}';
$data = json_decode($json );
$request = $data->request_list;
foreach($request as $request_data){
echo $request_data->id;
echo $request_data->name;
echo $request_data->surname;
}
My json-data is as follows -
[{"name": "ram"}]
What I want is the value of name in a variable e.g., $fname. I have tried -
<?php
$jsondata = '[{"name": "ram"}]';
//$obj = json_decode($jsondata);
$obj = json_decode($jsondata,true);
print_r($obj); // This line outputs as :- Array ( [0] => Array ( [name] => ram ) )
//What I need is value of key
print_r($obj['name']);
foreach ($obj as $k=>$v){
echo $v;
}
?>
But I am unable to get desired output.
Here how to get that value
<?php
$jsondata = '[{"name": "ram"}]';
$obj = json_decode($jsondata,true);
//In case have multiple array
foreach ($obj as $k){
echo $k['name'];
}
//else
$obj[0]['name'];
//if
$jsondata = '{"name": "ram"}';
$obj = json_decode($jsondata,true);
//use
echo $obj['name'];
?>
As your output indicates, your JSON string represents an array containing one object. As you know that you want a value contained within the first element, you can get it directly:
$jsondata = '[{"name": "ram"}]';
$obj = json_decode($jsondata, true);
$name = $obj[0]['name'];
echo $name;
I have a json file that I want to read with PHP
http://rh.ernestek.cz.cc/webeast/static.txt
I wanted to extract the "id" and the "key"
I wanted to loop through the id and key so that I can produce a table like this:
ID Key
1 9d07d681e0c1e294264724f7726e6f6f29
3 9cd1e3a4a04b5208862c3140046f73b515
...
I tried to extract the first id and key out using the following code but no luck:
<?php
$json = file_get_contents('static.txt');
$json_decoded = json_decode($json);
echo "ID: ".$json_decoded->static->1->id."<br />";
echo "Key: ".$json_decoded->static->1->key."<br />";
?>
Is there anything I an wrong?
Any ideas?
Thanks!
Decode the json as an array (pass true as second parameter), and loop over the array like so
<?php
$json = file_get_contents('static.txt');
$json_decoded = json_decode($json, true);
foreach ($json_decoded['static'] as $item) {
echo 'ID: ', $item['id'], '<br/>';
echo 'Key: ', $item['key'], '<br/>';
}
By using true in json_decode() it will convert all objects to array:
$json = file_get_contents("http://rh.ernestek.cz.cc/webeast/static.txt");
$json_decoded = json_decode($json, true); // return array
$table = array();
foreach($json_decoded["static"] as $array){
$table[$array["id"]] = $array["key"];
}
//creating the table for output
$output = '<table border="1"><tr><td>ID</td><td>Key</td></tr>';
foreach($table as $id => $key){
$output .= "<tr><td>$id</td><td>$key</td></tr>";
}
$output .= '</table>';
echo $output;