output an array using PHP - php

I have the following PHP code:
while($row = mysqli_fetch_array($query))
{
$data = $row['name'];
}
I fetch all the data with the column name "name" in database. How can I output it like this?
["John", "Doe", "Deer"]

You have to make $data as array type variable.
while($row = mysqli_fetch_array($query))
{
$data[] = $row['name'];
}
print_r($data); // required output

while($row = mysqli_fetch_array($query))
{
$data[] = $row['name'];
}
print_r($data); // output key wise display like
Array ( [0] => John [1] => Doe ) etc.
But output as you suggestion then just add json_encode()
print_r(json_encode($data)); // output like
["John", "Doe", "Deer"]

$data = [];
while($row = mysqli_fetch_array($query))
{
$data = $row['name'];
}
echo json_encode($data); // or you can use print_r for debugging.

You need to use json_encode() method to your array to be accepted in your jquery. Rearrange the code like following..
$data = array();
while($row = mysqli_fetch_array($query))
{
$data[] = $row['name'];
}
$new_array = json_encode($data);
echo $new_array; // use 'echo' to print. The json_encode() convert $data array to string.

Related

correct way to return a json_object as an array

I have an array populated using an sql statement in the following manner:
$index = 0;
while ($row = sqlsrv_fetch_array($stmt, SQLSRV_FETCH_ASSOC))
{
$bookname[$index] = ($row['Bookname']);
$subjectname[$index] = ($row['SubjectName']);
$index++;
}
When I go to echo json encode the Arrays I get a blank [] when I know it has been populated which is really weird.
Am I doing anything wrong in my context
echo json_encode($Bookname,$SubjectName);
You can use json_encode as like that:
<?php
$index = 0;
$data = array();
while ($row = sqlsrv_fetch_array($stmt, SQLSRV_FETCH_ASSOC))
{
$data[$index]['bookname'] = $row['Bookname'];
$data[$index]['subjectname'] = $row['SubjectName'];
$index++;
}
json_encode($data); // encode your array
?>
Try following:
$index = 0;
$data = array();
while ($row = sqlsrv_fetch_array($stmt, SQLSRV_FETCH_ASSOC))
{
$data['Bookname'][$index] = $row['Bookname']
$data['SubjectName'][$index] = $row['SubjectName'];
$index++;
}
echo json_encode($data);
You have passed two parameter while calling json_encode function. You batter combine two array in one. Then call the json_encode function like json_encode($combinedArray)

PHP create nested array and pass back with json_encode

I have the following, and while $data['details'] is being populated OK, there should be three results in $data['tests'], but "tests" isn't showing at all in the JSON result:
{"details":["Clinical result","Signature result"]}
$data = array();
while ($row = mysql_fetch_array($result)) {
$data['details'] = array($row['tests_clinical'], $row['signature']);
foreach($row['lab_test_group_fk'] as $group){
$data['tests'] = array($group);
}
}
echo json_encode($data);
If I change the above to the following then I get only the last record for $row['lab_test_group_fk'], not all three records for that column (hence the foreach loop as above):
while ($row = mysql_fetch_array($result)) {
$data['details'] = array($row['tests_clinical'], $row['signature']);
$data['tests'] = array($row['lab_test_group_fk']);
}
echo json_encode($data);
{"details":["Clinical result","Signature result"],"tests":["21"]}
What am I doing wrong here?
Thanks to Tamil Selvin this was the solution that worked for me:
$data = array();
while ($row = mysql_fetch_array($result)) {
$data['details'] = array($row['tests_clinical'], $row['signature']);
$data['tests'][] = array($row['lab_test_group_fk']);
}
echo json_encode($data);
Which returned:
{"details":["Clinical result","Signature result"],"tests":[["31"],["2"],["21"]]}
Try
$data = array();
while ($row = mysql_fetch_array($result)) {
$data[]['details'] = array($row['tests_clinical'], $row['signature']);
$data[]['tests'] = array($row['lab_test_group_fk']);
}
echo json_encode($data);
or
while ($row = mysql_fetch_array($result)) {
$data[] = array(
'details' => array($row['tests_clinical'], $row['signature']),
'tests' => array($row['lab_test_group_fk'])
);
}
echo json_encode($data);
$data['tests'] = array($group); means reassign $data['tests'] to a new value again.
Try $data['tests'][] = array($group); .
You are overwriting existing data of $data. You need some kind of array where you will put all your records. Try this
$data = array();
while ($row = mysql_fetch_array($result)) {
$record = array(); // this creates new record
$record['details'] = array($row['tests_clinical'], $row['signature']);
foreach($row['lab_test_group_fk'] as $group){
$record['tests'] = array($group);
}
$data[] = $record; // this adds record to data
}
echo json_encode($data);

How can I format my JSON in this way?

I'm trying to output JSON in this format:
[{"name":"venue 1"}, {"name":"venue 2"}, {"name":"venue 3"}]
But it's currently coming out like this:
{"name":"venue 1"}{"name":"venue 2"}{"name":"venue 3"}
Here is my code:
query = "SELECT * FROM venues";
$result = mysqli_query($con, $query);
while ($row = mysqli_fetch_array($result, MYSQLI_BOTH)) {
$arr = array(
'name'=> $row['name']
);
print json_encode($arr);
What do I need to change?
Add $arr[] intead of $arr
while ($row = mysqli_fetch_array($result, MYSQLI_BOTH)) {
$arr[] = array('name'=> $row['name'] );
}
echo json_encode($arr);
Try this:
$arr[] = array(
'name'=> $row['name']
);
Simply make an array like
$array[]["name"]="value1";
$array[]["name"]="value2";
$array[]["name"]="value3";
echo json_encode($array);
you will get the json data as:
[{"name":"value1"},{"name":"value2"},{"name":"value3"}]
which you wants. You have to make the array using looping statements according to your need.

output json array in php

I have this json currently :
{"quest_id":"1","quest_title":"Buy 3 pints of draft and a large pizza and then get desert","quest_price":"15","quest_points":"100"}{"quest_id":"2","quest_title":"Hello WOrld","quest_price":"50","quest_points":"10"}
I was wondering how I could output this :
{"quests": {"quest_id":"1","quest_title":"Buy 3 pints of draft and a large pizza and then get desert","quest_price":"15","quest_points":"100"}{"quest_id":"2","quest_title":"Hello WOrld","quest_price":"50","quest_points":"10"}
}
Here is the code in php:
while($result=mysql_fetch_array($number, MYSQL_ASSOC)){
print(json_encode($result));
}
Try this:
$result = array('quests' => array());
while($row = mysql_fetch_array($number, MYSQL_ASSOC)){
$result['quests'][] = $row
}
echo json_encode($result);
If I understand correctly what you are trying to do, get a JSON packet with all the rows, then loop over them to put them in an array, then encode the whole array:
<?php
$result = mysql_query($query);
$out = array('quests' => array());
while ($row = mysql_fetch_assoc($result)) {
$out['quests'][] = $row;
}
print json_encode($out);
?>

Storing data from SQL in array

I am trying to store the data from my sql database into an array. Currently I have this:
$query = mysql_query("SELECT * FROM `InspEmail` WHERE `Company` LIKE '$company'");
while($row = mysql_fetch_array($query))
{
$inspector = $row['name'];
}
The problem is that I have 8 rows of data. I need to store each 8 names from that database into an array. When I try this:
$inspector = array($row['name']);
It doesn't work.
If you want to store all of the names in an array, you need to define the array outside the scope of the while loop and append to it. Like this:
$nameArray = array();
while($row = mysql_fetch_array($query)) {
// Append to the array
$nameArray[] = $row['name'];
}
What you want is:
$inspector[] = $row['name'];
This will store all 8 names in an array similar to:
array(
[0] => name1
[1] => name2
[2] => name3
)
Lots of good answers. But if you do this often, you might want to write a little function:
mysql_field_array($sql, $fieldname)
{
$res = mysql_query($sql);
$a = array();
while($row = mysql_fetch_array($res))
{
$a[] = $row[$fieldname];
}
mysql_free_result($res);
return $a;
}
Replace this line...
$inspector = $row['name'];
...with this:
$inspector [] = $row['name'];
After that, the inspector array contains all the names.
$query = mysql_query("SELECT * FROM `InspEmail` WHERE `Company` LIKE '$company'");
$data = array();
while($row = mysql_fetch_array($query))
{
$inspector[] = $row;
}
for($i=0;$i<mysql_num_rows($row);$i++)
{
$data = $inspector[$i];
}
return $data;
Check it...

Categories