Json array add onto php array - php

I want to acheive something simliar to this adding elements onto an array but when I use this methods two nodes get created in the json element. I only want one node with all the entires within that also can you name nodes ie Properties.
$json = array();
while($row = mysql_fetch_assoc($sth)) {
$json['name'] = $row['name'];
$json['id'] = $row['id'];
$data[] = $json;
}
$custom = array('name'=>'foo', 'id' => 'bar');
$data[] = $custom;

Try this code array_push is better option here,
<?php
$json = array();
while($row = mysql_fetch_assoc($sth)) {
$temp = array();
$temp = array('name' => $row['name'], 'id' => $row['id']);
array_push($json, $temp);
}
$custom = array('name'=>'foo', 'id' => 'bar');
array_push($json,$custom);
?>

Related

Array json from php is not output in the corect way

I'm trying to make a json output from my db in php with all of users from sql table but i don't know how to do that..i'm new with this.
I want the code to output like this.
[
{
"name": "Maybell",
"avatar": "zeldman/128.jpg",
"data": {
"company": "Alibaba",
"title": "Engineer"
}
}
]
but the my code does the following output and is not ok.
{
"name": "Maybell",
"avatar": "zeldman\/128.jpg",
"company": "alibaba"
"title": "Engineer"
}
Here is the Php code:
<?php
header("Content-type: text/json");
$db = mysqli_connect("localhost","root","","testest");
//MSG
$query = "SELECT * FROM mls_users LIMIT 20";
$result = mysqli_query($db, $query);
//Add all records to an array
$rows = array();
while($row = $result->fetch_array()){
$name = $row['name'];
$avatar = $row['avatar'];
$company= $row['company'];
$title= $row['title'];
}
//Return result to jTable
$qryResult = array();
$qryResult['name'] = $name;
$qryResult['avatar'] = $avatar;
$qryResult['company'] = $company;
$qryResult['title'] = $title;
echo json_encode($qryResult,JSON_PRETTY_PRINT);
mysqli_close($db);
?>
Just change:
//Add all records to an array
$rows = array();
while($row = $result->fetch_array()){
$name = $row['name'];
$avatar = $row['avatar'];
$company= $row['company'];
$title= $row['title'];
}
//Return result to jTable
$qryResult = array();
$qryResult['name'] = $name;
$qryResult['avatar'] = $avatar;
$qryResult['company'] = $company;
$qryResult['title'] = $title;
To:
//Add all records to an array
$qryResult = [];
while ($row = $result->fetch_array()) {
$qryResult[] = [
'name' => $row['name'],
'avatar' => $row['avatar'],
'data' => [
'company' => $row['company'],
'title' => $row['title'],
],
];
}
You can skip setting the intermediate variables and just add onto the $qryResult array directly.
try this:
<?php
header("Content-type: text/json");
$db = mysqli_connect("localhost","root","","testest");
//MSG
$query = "SELECT * FROM mls_users LIMIT 20";
$result = mysqli_query($db, $query);
//Add all records to an array
$users = array();
while($row = $result->mysqli_fetch_assoc()){
$users[] = [
'name' => $row['name'],
'avatar' => $row['avatar'],
'data' => [
'company' => $row['company'],
'title' => $row['title']
]
]
}
echo json_encode($users,JSON_PRETTY_PRINT);
mysqli_close($db);
?>
I changed fetch_row() to mysqli_fetch_assoc() and then actually made an array with all fetched rows. You kind of wanted to do that i can see that in comments but you didnt. Then just json encode it.
The outlining brackets [ and ] should come automatically when the array has multiple elements in it.
You are almost there, you just need to wrap it in an array and make data an array.
$qryResult = array();
$qryResult['name'] = $name;
$qryResult['avatar'] = $avatar;
// Now make an array to hold data
$data = array();
$data['company'] = $company;
$data['title'] = $title;
// Add data to qryResult
$qryResult['data'] = $data
// Wrap qryResult in array so output will be wrapped in array
$outPutResults = array($qryResult);
echo json_encode($outPutResults,JSON_PRETTY_PRINT);
$qryResult = array();
$qryResult['name'] = $name;
$qryResult['avatar'] = $avatar;
$qryResult['data']['company'] = $company;
$qryResult['data']['title'] = $title;
Try that way. You are asking for a multidimensional-array so you need to set it up as one.

PHP how to make one sqaure bracket for all json object

The below is my php code.Many thanks
$data = array();
foreach ($row as $rowk) {
$data[] = array(
'message' => $row['traditionalmessage'],
'phone' => $row['telMobile']
);
break;
}
echo json_encode($data);
My current result
[{"message":"B\u578b\u809d\u708e\u75ab\u82d7\u4e0b\u4e00\u500b\u6ce8\u5c04\u671f\u4e3a2017-06-30\r\n","phone":"96709394"}][{"message":"\u75ab\u82d7\u540d\u7a314\u4e0b\u4e00\u500b\u6ce8\u5c04\u671f\u4e3a2017-06-30\r\n","phone":"96709394"}][{"message":"\u4fe1\u606f","phone":"55503234"}]
My desired result
[{"message":"B\u578b\u809d\u708e\u75ab\u82d7\u4e0b\u4e00\u500b\u6ce8\u5c04\u671f\u4e3a2017-06-30\r\n","phone":"96709394"},{"message":"\u75ab\u82d7\u540d\u7a314\u4e0b\u4e00\u500b\u6ce8\u5c04\u671f\u4e3a2017-06-30\r\n","phone":"96709394"},{"message":"\u4fe1\u606f","phone":"55503234"}]
In JSON arrays represented in [], and objects in {}, so in desired case you should have array of objects while you have array of arrays.
Try something like this:
$data = array();
foreach ($row as $rowk) {
$obj = new stdClass();
$obj->message = $row['traditionalmessage'];
$obj->phone = $row['telMobile'];
$data[] = $obj;
}
echo json_encode($data);

output an array using 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.

Php json_encode from mysql > output without column names

this is my code on a php page connected to mysql server.
$temp = array();
while($row = mysqli_fetch_assoc($result)) {
$temp[] = $row;
}
echo json_encode($temp);
The output is:
[{"column1":"1448741941","column2":"951"},{"column1":"1448747281","column2":"862"}]
That's is including the column title + data, and i wanna know how can i get only datas, like
[[1448741941,951],[1448747281,862]]
Thanks for the help!
Here is the quick answer, send in an array with the data only:
$temp = array();
while($row = mysqli_fetch_assoc($result)) {
$temp[] = array( $row['column1'], $row['column2'] );
}
echo json_encode($temp);
Edit You might actually whant this too JSON_NUMERIC_CHECK:
$temp = array();
while($row = mysqli_fetch_assoc($result)) {
$temp[] = array( $row['column1'], $row['column2'] );
}
echo json_encode($temp, JSON_NUMERIC_CHECK);
Another way is this:
$temp[] = array(
intval( $row['column1'] ),
intval( $row['column2'] ) );

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);

Categories