Here i am fetching data using php code.But i want the same code to be convert into json format.I am not getting the way to do it.
I have tried it in this way.
my code
while ($rows = mysql_fetch_array($filter)) {
$result['name'] = $rows['name'];
$result['id'] = $rows['id'];
}
$res = json_encode($result);
print_r($res);
getting result
{"name":["sradha","nicky","demo","testing"],"id":["1","2","3","4"]}
Here i want it in this below json format
[{id:1, name:'sradha'},
{id:2, name:'nicky'},
{id:3, name:'demo'},
{id:3, name:'testing'}];
Please suggest me.
Any suggestion will highly appreciate.
Thank you in advance.
Try below:
while ($rows = mysql_fetch_assoc($filter)) {
$result['name'] = $rows['name'];
$result['id'] = $rows['id'];
$new_result[] = $result;
}
$res = json_encode($new_result);
print_r($res);
OR
while ($rows = mysql_fetch_assoc($filter)) {
$result[] = $rows;
}
$res = json_encode($result);
print_r($res);
Try this
$result = array();
while ($rows = mysql_fetch_assoc($filter)) {
$result[] = $rows;
}
echo json_encode($result);
Create your array like this
$i=0;
while($rows = mysql_fetch_array($filter)) {
$result[$i]['name'] = $rows['name'];
$result[$i]['id'] = $rows['id'];
$i++;
}
$res = json_encode($result);
print_r($res);
It will return output result
Related
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)
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);
I want to output like this
[
['Alice', 'Mike'],
['Bob', 'Jim'],
['Carol', 'Bob']
]
but i could not get the format
here is my code
$cmd = $this->connection->prepare('SELECT emp,manager from department');
$cmd->execute();
$records = $cmd->fetchAll(PDO::FETCH_ASSOC);
foreach($records as $row) {
$rowdata=$row;
}
$return_data = array($rowdata);
echo json_encode($return_data);
Thank you in advance
update: Answer
$rowdata=array_values($row);
Thank you #Ja͢ck
Try this:
$records = $cmd->fetchAll(PDO::FETCH_NUM);
foreach($records as $row) {
$rowdata[] = array_values($row);
}
If you use <pre> tags around the array and use print_r() to print it, it will come out as very readable.
Here you can check
foreach($records as $row) {
$rowdata[]=$row;
}
$return_value=array_chunk($rowdata,2);
echo json_encode($return_value);
Update :
$cmd = $this->connection->prepare('SELECT emp,manager from department');
$cmd->execute();
$records = $cmd->fetchAll(PDO::FETCH_OBJ);
$rowdata=array();
foreach($records as $record) {
$rowdata[] = array($record->em.','.$record->manager);
}
$return_data = $rowdata;
echo json_encode($return_data);
Hi all Im trying to query a database and store the results ($searchResults[]) as an array :
<?php
if(isset($_POST['indexSearchSubmit']))
{
foreach($_POST['industryList'] as $selected)
{
$_POST['industryList'] = $selected;
$locationListResults = $_POST['locationList'];
$results = mysqli_query($con,"SELECT * FROM currentListings
WHERE location = '$locationListResults' AND industry = '$selected'");
$searchResults = array();
while($row = mysqli_fetch_array($results))
{
$searchResults[] = $row['industry'];
$searchResults[] = $row['location'];
$searchResults[] = $row['title'];
$searchResults[] = $row['description'];
}
}
mysqli_close($con);
}
?>
the problem im getting is when I try to echo the result:
<?php
echo $searchResults[0];
?>
its only bringing back 1 result not displaying all the results in the arrray as i want it to.
Could anybody please point out what it is im doing wrong.
Any help would be greatly appreciated
Do like this
<?php
print_r($searchResults); // Prints all array elements
?>
Alternatively, you can make use of a for loop to echo all elements too..
foreach($searchResults as $k=>$v)
{
echo $v;
echo "<br>";
}
Your code puts your data into 1D array. You probably want sth else so instead of this:
$searchResults[] = $row['industry'];
$searchResults[] = $row['location'];
$searchResults[] = $row['title'];
$searchResults[] = $row['description'];
do this:
$tmp = array();
$tmp['industry'] = $row['industry'];
$tmp['location'] = $row['location'];
$tmp['title'] = $row['title'];
$tmp['description'] = $row['description'];
$searchResults[] = $tmp;
or just this (thanks to Barmar):
$searchResults[] = $row;
This way you store your data as 2D array. So every row you obtain remains in one subarray.
To print the row (which is now in 2D array) iterate over subarrayw:
foreach($one_of_searchResult_rows as $k => $v)
{
// do whatever you need with $k and $v
}
I think you want a 2d array. Try this code snippet :
$searchResults = array();
while($row = mysqli_fetch_array($results))
{
array_push($searchResults,$row);
}
This should push each row as an associative array in every cell of your final searchResuts array. You could then access the values like:
echo $searchResults[0]['industry']; //and so on
echo $searchResults[0]; //Should print out the first match/row of the sql result
Lets make it simple :
$serach_result=mysqli_fetch_all ($result,MYSQLI_NUM);
//you should use print_r while trying to print an array
print_r($search_result[0]);
Reference : mysqli_fetch_all
$selected_offer = $_POST['selected_offer'];
$get_categories = $db->query("SELECT oc_id, oc_name FROM object_category WHERE oc_relate = '".$selected_offer."'");
$json = array();
while ($get_rows = mysql_fetch_array($get_categories, MYSQL_ASSOC)) {
$json[] = $get_rows;
}
echo json_encode($json);
return;
I toke this code from someone else and since I am not familiar with json I am asking here at stackoverflow how can add a function to the oc_name attribute before the json encodes it and still return the same struckture as it is now, like for example:
language($get_rows['oc_name'])
while ($get_rows = mysql_fetch_array($get_categories, MYSQL_ASSOC)) {
$get_rows['oc_name'] = language($get_rows['oc_name']);
$json[] = $get_rows;
}
You can apply your function on the mentioned field before you add the row in your $json array
$json = array();
while ($get_rows = mysql_fetch_array($get_categories, MYSQL_ASSOC)) {
$get_rows['oc_name']=language($get_rows['oc_name']);
$json[] = $get_rows;
}