I am trying to get my JSON output like this.
{"allterms":[{"group":{"Name":"Test 1"},{"group":{"Name":"Test2","Id":"298"}}]
My current code is
while($r = mysql_fetch_assoc($rs)) {
$rows['allterms']['group'][] = $r;
}
Which gives me this
{"allterms":{"group":[{"Name":"Test1", "Id":"1740"},{"Name":"Test2","Id":"631"}}]
How can I adjust my code so that each item has a parent term group.
Change the loop like so:
while($r = mysql_fetch_assoc($rs)) {
$rows['allterms'][]['group'] = $r;
}
which will generate:
array(
'allterms' => array(
0 => array(
'group' => array(...),
),
1 => array(
'group' => array(...),
)
...
)
which as json will be:
{
"allterms": [
{
"group": {
{
"Name": "Test1",
"Id": "1740"
},
{
"group": {
{
"Name": "Test2",
"Id": "631"
}
}
]
}
You could use the array_push() function from PHP.
while($r = mysql_fetch_assoc($rs)) {
array_push($rows['allterms'], $r);
}
Related
I have the code below, but the result of the json response doesn't match
public function tes_get(){
$kode= $this->M_mymodel->tbl_kode('302'); // row
$res = array();
foreach ($kode as $key=> $value) {
$win = $this->M_mymodel->db_aa('302'); //row_array
$res = $this->M_mymodel->db_bb('302','LOSE'); //row_array
$res['data'][] = array(
'win' => $win['menang'],
'lose' =>$res['kalah']
);
}
$response = $this->set_response($res,200);
}
Below is the result of the response from the code I made above
{
"data": [
{
"win": "2",
"lose": "11"
}
]
}
How to make json response like below?
{
"data": [
{
"win": "2",
}
{
"lose": "11"
}
]
}
You can try :
$res['data'][] = array(
array('win' => $win['menang']),
array('lose' =>$res['kalah'])
);
I have php script which is storing data into an array and then converting into json array. Following is script
$gameTraining = array();
$index = 0;
foreach($todayTraining->toArray() as $training){
if($todayTraining[$index]['type'] === 'easy'){
$gameTraining['easy'][]['game_id'] = $todayTraining[$index]['game_id'];
}
$index++;
}
return $gameTraining;
And following response I am getting
{
"training": {
"easy": [
{
"game_id": 12
},
{
"game_id": 6
},
{
"game_id": 26
}
]
}
}
But I would like to remove the brackets from array, so can you kindly guide me how can I do that? I would like to convert as following
{
"training": {
"easy": [
"game_id": 12,
"game_id": 6,
"game_id": 26
]
}
}
You cannot have multiple items in an array with the same key. You can make an array with the ids for the game, so this line:
$gameTraining['easy'][]['game_id'] = $todayTraining[$index]['game_id'];
can be changed with this line:
$gameTraining['easy']['game_ids'][] = $todayTraining[$index]['game_id'];
Try something like this:
$todayTraining = [
[
'type' => 'easy',
'game_id' => 123
],
[
'type' => 'easy',
'game_id' => 456
]
];
$gameTraining = array();
$index = 0;
foreach($todayTraining as $training){
if($todayTraining[$index]['type'] === 'easy'){
$gameTraining['easy'][] = substr(json_encode(['game_id' => $todayTraining[$index]['game_id']]), 1, -1);
}
$index++;
}
echo json_encode($gameTraining, JSON_PRETTY_PRINT);
Just use below line :
$gameTraining['easy'][] = $todayTraining[$index]['game_id'];
Instead of :
$gameTraining['easy'][]['game_id'] = $todayTraining[$index]['game_id'];
Hopefully, it will work.
I am using following code for making data coming from database as json format
public function employeeSearch()
{
$arrayOfEmployee = array();
$arrayToPush = array();
$arrayToJSON = array();
$new_item = $this->apicaller->sendRequest(array(
"controller" => "Employee",
"action" => "employeeSearch",
"searchCriteria" => "12345"
));
$arrayOfEmployee = json_decode($new_item,true);
foreach($arrayOfEmployee as $key => $employee)
{
$arrayToPush = array('data' => $employee['FullName'], 'value' => $employee['_id']['$oid']);
array_push($arrayToJSON, $arrayToPush);
}
echo json_encode($arrayToJSON);
}
The output is
[{"data":"Aasiya Rashid Khan","value":"5aa662b0d2ccda095400022f"},
{"data":"Sana Jeelani Khan","value":"5aa75d8fd2ccda0fa0006187"},
{"data":"Asad Hussain Khan","value":"5aaa51ead2ccda0860002692"},
{"data":"Ayesha Khan Khann","value":"5aab61b4d2ccda0bc400190f"},
{"data":"adhar card name","value":"5aaba0e1d2ccda0bc4001910"}
]
Now I want that json elements should look like
{
"suggestions": [
{
"value": "Guilherand-Granges",
"data": "750"
},
{
"value": "Paris 01",
"data": "750"
}
]
}
I have to implement this in jQuery autocomplete plugin...
Please help!!!
Replace the last line with
echo json_encode(["suggestions" => $arrayToJSON]);
This should result in the wanted result!
(This hold only true if you igonre the fact that the data in value and name is not the same/similar)
I am New to PHP I want to generate a json code like this
[{"section_name": "Section A","data": [{"value": "2"},{"value": "0"}]}, {"section_name": "Section B","data": [{"value": "1"},{"value": "0"}]}]
In this I am retrieving "section_name","value" from database.
Here the concept is get values from database to display a bar chart, based on classes and sections.
But my template I had chosen, I have to get the values(no. of students in a class & section) for individual sections, as shown in below bar chart.
My PHP Code:
$result1 = mysql_query("select class_name as label from class ") or die(mysql_error());
while($row1 = mysql_fetch_assoc($result1))
{
$rows1[] = $row1;
}
$data1 = json_encode($rows1);
$result2 = mysql_query("select s.section_name as sectionname, 'data' as data from section s") or die(mysql_error());
$result3 = mysql_query("select (select count(*)as c from student_status ss where ss.section_id=s.section_id and ss.class_id=c.class_id) as value from section s, class c order by s.section_name asc ") or die(mysql_error());
if(mysql_num_rows($result2) > 1)
{
while($row2 = mysql_fetch_assoc($result2))
{
$rows2[] = $row2;
}
$data2 = json_encode($rows2);
while($row3 = mysql_fetch_assoc($result3))
{
$rows3[] = $row3;
}
$data3 = json_encode($rows3);
}
My JSON CODE:
"categories": [
{
"category": <? echo $data1 ?>/*[
{
"label": "1"
},
{
"label": "TWO"
},
{
"label": "3"
}
]*/
}
],
"dataset": /*<? echo $data2 ?>*/[
{
"sectionname": "Section A",
"data": [
{
"value": "2" //class 1
},
{
"value": "1" //class 2
}
]
},
{
"sectionname": "Section B",
"data": [
{
"value": "" //class 1
},
{
"value": "" //class2
}
]
}
]
Hope you guys understand question.
If you are new with php, you should read array in php manual.
Then you can define an array like this
$arr = array(
array(
'section_name' => 'Section A',
'data => array(
array('value' => 2),
array('value' => 0),
)
),
)
//... other section
Then output json echo json_encode($arr);
[] is used for array and {"a": ..} for associative arrays. Combining this two together and you could easily create this json structure.
array(
array(
"section_name" => "Section A",
"data" => array(
array("value" => "2"),
array("value" => "0"),
)
),
// ...
)
Using json_encode gives you the wanted result.
I'm trying to fix a problem in my JSON API when I request a multiple result set from my database. Basically I want to store a named array, "card" for each row returned. My goal is to get the JSON to look like this:
{
"results": [
{
"card": {
"property": "value",
"property": "value",
"property": "value"
}
},
{
"card": {
"property": "value",
"property": "value",
"property": "value"
}
}
]
}
But instead it looks like this:
{
"results": [
{
"card": [
{
"property": "value",
"property": "value",
"property": "value"
},
{
"property": "value",
"property": "value",
"property": "value"
}
]
}
]
}
Below you can find my code:
// Prepare Stored Procedure Call
$dbConn = new ConnDB();
$dbConn->query("CALL " . $this->proc);
//Get Result$$$
$row = $dbConn->resultset();
foreach ($row as $key => $value) {
$this->valueArray[$key] = $value;
}
$this->data = array(
"results" => array(
array(
$this->type => $this->valueArray
),
)
);
I need to basically build a named array, "card"=>$values, for each $row, but I can't do it in the middle of an array declaration, so the best thing I could think of was this:
// Prepare Stored Procedure Call
$dbConn = new ConnDB();
$dbConn->query("CALL " . $this->proc);
//Get Result$$$
$row = $dbConn->resultset();
$jsonArray = array();
foreach ($row as $key => $value) {
array_push($jsonArray, $this->type => $this->valueArray[$key] = $value)
}
$this->data = array(
"results" => array(
array(
$jsonArray
),
)
);
But that of course gives me parse errors. Argh! A bit frustrating.
Any ideas?
YES! I figured it out.
// Prepare Stored Procedure Call
$dbConn = new ConnDB();
$dbConn->query("CALL " . $this->proc);
//Get Result$$$
$row = $dbConn->resultset();
$jsonArray = array();
foreach ($row as $key => $value) {
array_push($jsonArray, array("card" => $value));
}
$this->data = array(
"results" => $jsonArray,
);
This returns the exact format I need. I was overthinking it.
Change your code as follows
<?php
$data = array();
$json_array = array();
$test_array = array(1,2,3,4,5,6,7,8,9,10);
foreach($test_array as $t){
$json_array[]['card'] = array('property' => $t);
}
$data['results'] = $json_array;
echo json_encode($data);
?>
Result
{"results":[{"card":{"property":1}},{"card":{"property":2}},{"card":{"property":3}},{"card":{"property":4}},{"card":{"property":5}},{"card":{"property":6}},{"card":{"property":7}},{"card":{"property":8}},{"card":{"property":9}},{"card":{"property":10}}]}