How to get json output in php from url - php

I see there is some more questions about this, but not useful for me.
Here i have the link and following code i am Link
<?php
$json=file_get_contents("http://2strok.com/radio.json");
$details=json_decode($json);
if($details->Response=='True')
?>
<?php echo $details->name[3];?><br>

$json=file_get_contents("http://2strok.com/radio.json");
$details=json_decode($json,true);
foreach($details as $output) {
echo $output['name'].'<br>';
}
Update - get specific name
$json=file_get_contents("http://2strok.com/radio.json");
$details=json_decode($json,true);
foreach($details as $output) {
if ($output['name']=='FM 101 / Islamabad'){
echo 'Specific name found: '.$output['name'];
}
// echo $output['name'].'<br>';
}

After json decode, it returns an array of objects, so try this:
$json=file_get_contents("http://2strok.com/radio.json");
$details=json_decode($json);
foreach ($details as $detail){
echo $detail->name;
echo "<br>";
}
?>

Related

How do i change the format of an SQL output?

I am using Laravel 5.5, and when I am trying to export data from another DB, i use:
$fname = DB::connection('smallplanet')->table('pilots')->where('id', Auth::user()->id)->get(['fname']);
echo $fname;
and this is the output:
[{"fname":"Aidas1"}]
how do i change the output to just "Aidas1"?
Its JSON ,try this:
$output = json_decode($fname,true);
echo $output[0]["fname"];
//********* OR **********
foreach($output as $row)
echo $row["fname"];
Couldn't it be easier to change the source instead of the result ?
For one pilot
$pilot = DB::connection('smallplanet')->table('pilots')->find(Auth::user()->id);
if($pilot){
echo $pilot->fname;
}else{
echo "pilot not found";
}
For many pilots
$pilots = DB::connection('smallplanet')->table('pilots')->where('your_field', $value)->get();
foreach($pilots as $pilot){
echo $pilot->fname . PHP_EOL;
}

How to print jSON values with loop

I am using the following code to print the output of the jSON response but when I try to print
echo $obj->HotelListResponse->customerSessionId; // This is working.
echo $obj->HotelListResponse->HotelList->HotelSummary->name; // This is not working.
When the response contains only one node then its printing perfectly but when there are multiple nodes with same name then its not printing. I tried using foreach just like the below. I also tried using while loop but still I am unable to print the list of hotel names.
My jSON decoded output is like http://pastebin.com/Fr21DkEk
Total code:
$url = "https://api.eancdn.com/ean-services/rs/hotel/v3/list?cid=55505&minorRev=99&apiKey=cbrzfta369qwyrm9t5b8y8kf&locale=en_AU&currencyCode=AUD&xml=<HotelListRequest><city>Brisbane</city><stateProvinceCode>QLD</stateProvinceCode><countryCode>AU</countryCode><arrivalDate>10/16/2014</arrivalDate><departureDate>10/18/2014</departureDate><RoomGroup><Room><numberOfAdults>2</numberOfAdults></Room></RoomGroup><numberOfResults>25</numberOfResults></HotelListRequest>";
$json = file_get_contents($url);
$obj = json_decode($json);
foreach($obj as $val) {
echo $val->HotelListResponse->HotelList->HotelSummary->name;
}
Try this
foreach($obj->HotelListResponse->HotelList->HotelSummary as $val) {
echo $val->name . '<br/>';
}
HotelSummary is an array:
echo $val->HotelListResponse->HotelList->HotelSummary[0]->name;
If you want all of the hotel summaries:
foreach($obj as $val) {
foreach($val->HotelListResponse->HotelList->HotelSummary as $sum) {
echo $sum->name;
}
}
Yes you can directly access them inside the foreach. Like this:
foreach($obj->HotelListResponse->HotelList->HotelSummary as $val) {
// ^^
// since you're interested on just names, you can point it directly on that object, then each of that batch is in `$val`
echo $val->name . '<br/>';
}
// or start from the parent
foreach($obj as $values) {
$customerSessionId = $values->customerSessionId;
echo $customerSessionId . '<hr/>';
$hotelList = $values->HotelList;
foreach($hotelList->HotelSummary as $hotelsummary) {
echo $hotelsummary->name . '<br/>';
}
}

Multiple arrays inside array, new div for each array inside an array

Okay so I have multiple arrays inside an array. For each array inside the array I want the inner array to to be echoed in a new div. I'm completely lost on how to do this
Also, it should not make a div if the array is empty.
This is the code I'm using to var_dump the following output:
This is the output:
I've read through php array documentation and searched on stackoverflow, I can't seem to find an answer so please help. Thanks!
foreach($video_by_game as $game_video)
if(count($game_video)) {
echo '<div>';
foreach($game_video as $game)
echo $game->title.'<br />';
echo '</div>';
}
Do not do a foreach $game_video[], do foreach $game_video.
foreach($videos_by_game as $game_video) {
foreach($game_video as $gv) {
// do your output
}
}
The $game_video is already an array. By doing $game_video[], you are trying to iterate on its first element
<?php foreach($video_by_game as $videos): // foreach game ?>
<?php foreach($videos as $video): // foreach video of the game ?>
<?php if (empty($video)) continue; // if no videos -/> skip ?>
<div>
Title: <?php echo $video->title ?> <br>
Added by: <?php echo $video->added_by ?> <br>
</div>
<?php endforeach ?>
<?php endforeach ?>
You can use array_walk function to process each item of $game_video array:
if (!empty($game_video)) {
echo "<div>";
array_walk($game_video, function($item,$key){
echo "The necessary output";
});
echo "</div>";
}

Echo selective data (rows) from multidimensional array in PHP

I have an array coming from a .csv file. These are coming from a real estate program. In the second column I have the words For Sale and in the third column the words For Rent that are indicated only on the rows that are concerned. Otherwise the cell is empty. I want to display a list rows only For Sale for example. Of course then if the user clicks on a link in one of the rows, the appropriate page will be displayed.
I can't seem to target the text in the column, and I can't permit that the words For Sale be used throughout the entire array because they could appear in another column (description for example).
I have tried this, but to no avail.
/* The array is $arrCSV */
foreach($arrCSV as $book) {
if($book[1] === For Sale) {
echo '<div>';
}
echo '<div>';
echo $book[0]. '<br>';
echo $book[1]. '<br>';
echo $book[2]. '<br>';
echo $book[3]. '<br>';
echo $book[6]. '<br><br><br>';
echo '</div>';
}
I also tried this:
foreach($arrCSV as $key => $book) {
if($book['1'] == 'For Sale') {
echo '<div>';
}
echo '<div>';
echo $book[0]. '<br>';
echo $book[1]. '<br>';
echo $book[2]. '<br>';
echo $book[3]. '<br>';
echo $book[6]. '<br><br><br>';
echo '</div>';
}
Do you mean:
foreach($arrCSV as $key => $book) {
if($book['1'] == 'For Sale') {
echo '<div></div>';
}else{
echo '<div>';
echo $book[0]. '<br>';
echo $book[1]. '<br>';
echo $book[2]. '<br>';
echo $book[3]. '<br>';
echo $book[6]. '<br><br><br>';
echo '</div>';
}
}
I found a solution here:
PHP: Taking Array (CSV) And Intelligently Returning Information
$searchCity = 'For Sale'; //or whatever you are looking for
$file = file_get_contents('annonces.csv');
$results = array();
$lines = explode("\n",$file);
//use any line delim as the 1st param,
//im deciding on \n but idk how your file is encoded
foreach($lines as $line){
//split the line
$col = explode(";",$line);
//and you know city is the 3rd element
if(trim($col[1]) == $searchCity){
$results[] = $col;
}
}
And then:
foreach($results as $Rockband)
{
echo "<tr>";
foreach($Rockband as $item)
{
echo "<td>$item</td>";
}
echo "</tr>";
}
As you can see I'm learning. It turns out that by formulating a question, a series of similar posts are displayed, which is much quicker and easier than using google. Thanks.

How do I print all POST results when a form is submitted? [duplicate]

This question already has answers here:
Print out post values
(11 answers)
Closed 6 years ago.
I need to see all of the POST results that are submitted to the server for testing.
What would be an example of how I can create a new file to submit to that will echo out all of the fields which were submitted with that form?
It's dynamic, so some fields may have a name/ID of field1, field2, field3, etc.
All the values are stored in the $_POST collection
<?php print_r($_POST); ?>
or if you want something fancier that is easier to read use a foreach loop to loop through the $_POST collection and print the values.
<table>
<?php
foreach ($_POST as $key => $value) {
echo "<tr>";
echo "<td>";
echo $key;
echo "</td>";
echo "<td>";
echo $value;
echo "</td>";
echo "</tr>";
}
?>
</table>
You could try var_dump:
var_dump($_POST)
Simply:
<?php
print_r($_POST);
//Or:
foreach ($_POST as $key => $value)
echo $key.'='.$value.'<br />';
?>
You may mean something like this:
<?php
$output = var_export($_POST, true);
error_log($output, 0, "/path/to/file.log");
?>
You could use something as simple as this
<?php
print_r($_POST);
?>
This would make it a bit more viewable:
<?php
echo str_replace(' ', ' ', nl2br(print_r($_POST, true)));
?>
You can definitely use var_dump, but you mentioned you are in front-end development. I am sure you would know this, but just as a reminder, use Firefox's Firebug or Chrome's / Internet Explorer's developers tool and check for the post. Post goes through hearders, and you should be able to check it from there too.
if (! function_exists('d'))
{
// Debugger
function d($var, $exit = 0)
{
// Only output on localhost
if ($_SERVER['HTTP_HOST'] != 'localhost')
{
return;
}
echo "\n[degug_output_BEGIN]<pre>\n";
echo var_export($var, 1);
echo "\n</pre>[degug_output_END]\n";
if ($exit)
exit;
}
}
// Call:
d($_POST);
Bonus: Check debug_backtrace() too add tracing to your debugging.

Categories