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);
Related
i want return my data from mysql to json array with do while loop.
when i return one record with json it is well. but i want return list of array in one json array.
how can do that with index in while? my code have error and i don't know how can do it. with one record it is well but more can't.
$json="array(";
do{
$json=."'$row_query['id']'=>array('fname'=>$row_query['fname'],'lname'=>$row_query['lname']),";
} while ($row_query = mysqli_fetch_assoc($query));
json=.")";
echo json_encode($json,JSON_UNESCAPED_UNICODE);
There's no need to add quotes, brackets, parentheses or whatever. json_encode function will do it for you. Just provide an array to it:
$json = [];
while ($row_query = mysqli_fetch_assoc($query)) {
$json[$row_query['id']] = [
'fname'=>$row_query['fname'],
'lname'=>$row_query['lname'],
];
}
echo json_encode($json,JSON_UNESCAPED_UNICODE);
This is the most super easy version. You may try this.
$json = new array();
do{
array_push($json,$row_query);
} while ($row_query = mysqli_fetch_assoc($query));
or
$json = [];
do{
$json[] = $row_query;
} while ($row_query = mysqli_fetch_assoc($query));
Atlast encode your json variable.
$json = json_encode($json);
echo $json;
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);
I need json response in different array, but below code not give all the result.
while ($row=mysqli_fetch_assoc($result))
{
//$data[]=$row;
$data['id']=$row['id'];
$data['name']=$row['name'];
$data['Latitude']=$row['Latitude'];
$data['Longitude']=$row['Latitude'];
$lat_user=$row['Latitude'];
$long_user=$row['Longitude'];
$res=file_get_contents("https://maps.googleapis.com/maps/api/distancematrix/json".
"?units=imperial&origins=$lat,$long&destinations=$latuser,$longuser");
$json_res=json_decode($res);
$data['time']=$json_res->{'rows'}{0}->{'elements'}{0}->{'duration'}->{'text'};
}
echo json_encode($data,true);
You need to save your items into some array, e.g $dataArray at the end of each iteration and after cycle use this array to create json
$dataArray = array();
while ($row=mysqli_fetch_assoc($result))
{
//$data[]=$row;
$data['id']=$row['id'];
$data['name']=$row['name'];
$data['Latitude']=$row['Latitude'];
$data['Longitude']=$row['Latitude'];
$lat_user=$row['Latitude'];
$long_user=$row['Longitude'];
$res=file_get_contents("https://maps.googleapis.com/maps/api/distancematrix/json".
"?units=imperial&origins=$lat,$long&destinations=$latuser,$longuser");
$json_res=json_decode($res);
$data['time']=$json_res->{'rows'}{0}->{'elements'}{0}->{'duration'}->{'text'};
$dataArray[] = $data;
}
echo json_encode($dataArray,true);
I am developing in PHP/MS SQL for getting JSON Response.
Code which I wrote is:
while( $result = sqlsrv_fetch_object($sql_Gpo_Carr)) {
$array_res[] = $result; // add result to array
array_push($array_res, array('unidad' => $uni)); // add extra element
$jsonObj = json_encode($array_res); // encode JSON
}
echo $jsonObj;
exit();
This is what I want in result:
[{"idperiodo":"37","idgrupo":"1963","idhorario":"12832","unidades":null,"unidad":1}]
but the result shows me this:
[{"idperiodo":"37","idgrupo":"1963","idhorario":"12832","unidades":null},{"unidad":1}]
You're fetching an object. Add $uni to $result first and then add to $array_res:
while( $result = sqlsrv_fetch_object($sql_Gpo_Carr)) {
$result->unidad = $uni;
$array_res[] = $result;
}
Also, you probably want the json_encode() after the loop not in the loop:
echo json_encode($array_res);
$responses = array();
while ($row = mysql_fetch_array($result)) {
$response = array(
'name' => $row['name']
);
$row;
$responses['name5'] = $response;
}
echo json_encode($responses);
I'm currently only getting 1 rows from this statement I know for a fact their are more.
On each iteration of your while loop, you are overwriting the same array key $responses['name5'], so in the end you'll only have one value in the $responses array.
Instead, you might want something like this to append to the end of the array:
$responses[] = $response;
you are overwriting the $response variable that's why, array_push instead
$responses['name5'] = $response;
You will get only last row because you replace your data each cycle step.
Try this:
$responses['name5'][] = $response;
Because you're resetting the $response array to a single array in the loop. You want to add to the array.
$responses = array();
while ($row = mysql_fetch_array($result)) {
array_push($response, array(
'name' => $row['name']
));
$row;
$responses['name5'] = $response;
}
echo json_encode($responses);
do
$responses[] = array('name5' => $response);