how to print json decode api data in laravel - php

i want to print data which is coming from api when i print data it print only one data at a time how to print all these data in foreach loop i am using this but not work and getting and error Trying to get property 'Name' of non-object how to solve this problems thanks in advance
this is the response
this is my code
$response = sabreApiCall($url, $data_array);
$results = json_decode($response);
$i =0;
foreach ($results->GeoSearchRS->GeoSearchResults->GeoSearchResult[$i] as $hotel)
{
echo $hotel->Name;
$i ++;
}
}

Just return $results as laravel will return it as json.

why are you using $i variable to loop through this array. Simply use foreach like this
$response = sabreApiCall($url, $data_array);
$results = json_decode($response);
foreach ($results->GeoSearchRS->GeoSearchResults->GeoSearchResult as $hotel)
{
echo $hotel->Name;
}
}

Related

Push array into array then encode - PHP

I'm trying to combine a few mySQL queries in a single PHP and push it all into a single JSON object.
So, I'm starting with the first query...like this:
$data=[];
$response = $stmt->fetchAll(PDO::FETCH_ASSOC);
$final_count = count($response);
$data['count'] = $final_count;
If I then do echo json_encode($data); I get a nicely formatted object like: {"count":61}
I then have a second query that I put the results through a loop, like so:
$response = $stmt->fetchAll(PDO::FETCH_ASSOC);
$items = array();
foreach ($response as &$value) {
$items[] = $value['date_added'];
}
echo json_encode($items);
And I get my nice set of dates:
["2017-06-24 00:08:58","2017-06-26 15:01:48","2017-06-27 15:01:48","2017-06-28 23:19:41","2017-06-29 01:38:07","2017-06-30 00:08:58"]
Here's the question, how do I get this all back together like so:
{
"count": 61,
"dates": [
"2017-06-24 00:08:58",
"2017-06-26 15:01:48",
"2017-06-27 15:01:48",
"2017-06-28 23:19:41",
"2017-06-29 01:38:07",
"2017-06-30 00:08:58"
]
}
You could use
$myData['count'] = $final_count;
$myData['dates'] = $items
echo json_encode($myData);
$data=[];
$response = $stmt->fetchAll(PDO::FETCH_ASSOC);
$final_count = count($response);
// first store count in `$data`
$data['count'] = $final_count;
$data['dates'] = [];
$response = $stmt->fetchAll(PDO::FETCH_ASSOC);
// next, store dates in subarray of `$data`
foreach ($response as &$value) {
$data['dates'][] = $value['date_added'];
}
// finally encode everything
echo json_encode($data);
Of course you can use array_merge of all your collected data.
Or:
$data=[];
// get $final_count
$data['count'] = $final_count;
// ... do some more stuff
// load items from db
$data['dates'] = $items;
echo json_encode($data);

Create JSON with function and FOR loop

I am trying to create a json response using a for loop and a function but I can't seem to get it to work correctly. How would this be done?
PHP
$count = 0;
foreach ($bookings as $booking){
$item = get_users_custom($user,$count);
$item[$count]['booking_id']=$booking->id;
$count++;
}
echo $item;
function get_users_custom($user,$count=0){
$feed[$count]['user_id']=$user->id;
return $feed;
}
PHP json_encode() function converts a PHP value into a JSON value. For example, from a PHP array, it can create a JSON representation of that array.
$count = 0;
foreach ($bookings as $booking){
$item = get_users_custom($user,$count);
$item[$count]['booking_id']=$booking->id;
$count++;
}
echo json_encode($item);
function get_users_custom($user,$count=0){
$feed[$count]['user_id']=$user->id;
return $feed;
}
You should create a simple php array and the convert it to JSON using json_encode()
https://www.w3schools.com/js/js_json_php.asp

PHP While loop for Select Query

Can anyone explain me how to get the final value assigned to variable after completing executing while loop?
In my below code I wanted to echo the response out of while loop after fetching all the values from the rows.
Because if I put echo out of while loop it only shows 1st record.
while ($row = oci_fetch_array($array)) {
$response = $row['0']->load();
echo $response;
}
You will get the last rows value into the $response.
Cause: Every time the loop executes will assign value into the variable. But as you echo it you can see all the values as output.
So what you really need to do is storing the values in an array...
$response = array();
while ($row = oci_fetch_array($array)) {
$response[] = $row['0']->load();
}
print_r($response);
If you need further information about this , just let me know.
Since you are doing variable assignment and echo inside while loop, it will not serve your purpose.
You have to do it like below:-
$response = array(); // create an array
while ($row = oci_fetch_array($array)) {
$response[] = $row['0']->load(); // assign each value to array
}
echo "<pre/>";print_r($response);// print the array
Note:- this array will have all the values now. You can manipulate it in your desired way.Thanks
while ($row = oci_fetch_array($array)) {
$response = $row['0']->load();
}
echo $response;
Try this:
$response = [];
while ($row = oci_fetch_array($array)) {
$response[] = $row['0']->load();
}
var_dump($response);

JSON decode in php web programming

I am facing a problem in php using CURL. I made a https request and I got response in multidimesional array.
[{
"id":"22622",
"name":"",
"email":"ffv7678#gmail.com",
"mobileno":"",
"birth_dt":"",
"marital_status":"",
"gender":"",
"educationid":"0",
"occupationid":"0",
"industryid":"0",
"incomeid":"",
"city":"0",
"state":"0",
"country":"0",
"postcode":"",
"deviceid":"805086099499488",
"regid":"",
"device_type":null,
"userstatus":"0",
"refcode":"D1219C92",
"new_user":"0",
"device_token":""
}]
Now I want to decode it and save the value of "id" in a variable.
$result=curl_exec($ch);
$books = json_decode($result, true);
echo ($books[0]['id']);
I tried the above code, but failed.
$books = json_decode($result, true);
foreach ($books as $book)
{
//$book carries info of books;
$id = $book['id'];
///... You can define other variables here
}
You need to use foreach() loop for this.
foreach ($a[0] as $key => $value) {
echo $key."<br>".$value;
if($key == 'id'){
$id = $value;
}
}
echo $id;
Also, if you just want to assign the value of id to another variable, you can just right
...
$no = $a[0]->id;
echo $no
...
instead of the foreach loop.

How to loop json and parse the value

This is my php code [EDITED]:
$redis_data = file_get_contents("http://example.org/data/data.txt");
That code returns the following json data:
{"Phone":"08XXXXX","StartTime":"121212","Customer":"Customer A","time":1407921302}
{"Phone":"08XXXXX0","StartTime":"111111","Customer":"Customer B","time":1407921302
{"Phone":"08XXXXX","StartTime":"131313","Customer":"Customer C","time":1407921302}
This is my code to looping parse the value:
$redis_data = file_get_contents("http://example.org/data/data.txt");
$redis_data = json_decode($redis_data, true);
foreach ($redis_data as $data) {
echo $data['Phone'];
echo "<br>";
}
But I got this errors:
Invalid argument supplied for foreach()
What am I doing wrong?
The data you trying to get is not well formatted json
[{"firstName":"John", "lastName":"Doe"},
{"firstName":"Anna", "lastName":"Smith"},
{"firstName":"Peter", "lastName": "Jones"}]
This is the json syntax.
and you are getting data in following format.
{"ANumber":"08122378673","BNumber":"500046","StartTime":"08122014102113","EndTime":"08122014104126","TrunkA":"S1JK2SBD1S","TrunkB":"N7JK2GSM1B","Customer":"PT.BNI","time":1407921302}
{"ANumber":"081351607600","BNumber":"14000","StartTime":"08122014102406","EndTime":"08122014103738","TrunkA":"S1JK2SSB1S","TrunkB":"EPJK2MNR0","Customer":"Bank Mandiri (CC_IB_Jasnita)","time":1407921302}
You may need to reformat your data string
You're trying to loop JSON - not PHP. You need to run json_decode (docs) on the your $redis_data variable before attempting to loop it.
$redis_data = json_decode($redis_data, true);
You need to decode the json before you can loop through it.
$redis_data = json_decode(file_get_contents("http://example.com/tes_files/data.txt"), true);
foreach ($redis_data as $data) {
echo $data['phone'];
}
Here is solution
$redis_data = explode( chr(10), file_get_contents("http://example.com/tes_redis/data.txt"));
foreach ($redis_data as $data) {
var_dump(json_decode($data));
$data = json_decode($data);
echo $data->Phone;
echo $data->StartTime;
echo $data->Customer;
//echo $data->ANumber;
//echo $data->BNumber;
}
you need to export data first.
first you have to convert json to array using json_decode function it will give array and after that you can loop
`
$jsonString =
'[{"Phone":"08XXXXX","StartTime":"121212","Customer":"Customer A","time":1407921302},
{"Phone":"08XXXXX0","StartTime":"111111","Customer":"Customer B","time":1407921302},
{"Phone":"08XXXXX","StartTime":"131313","Customer":"Customer C","time":1407921302} ]';
$array=json_decode($jsonString);
foreach ($arrayOfEmails as $data) {
echo $data->Phone."<br>";
}
`

Categories