I need help with my json output. At present my output is as so:
[
[
{
"ING_SW_CB": "5",
"SB_SW_CB": "3",
"NG3_SW_CB": "1",
"Mould_Close": "4",
"Leak_Test": "5",
"ML_Load": "6",
"Pre_Heat": "3",
"Dispense": "9",
"A310": "7",
............
}
]
]
I’d like it to be as following with "key" & "value" included in the output.
{"key":"ING_SW_CB", "value": "5"},
{"key":"SB_SW_CB", "value": "3"},
..............................
Hers is my PHP script:
$json_response = array();
while ($row = mysqli_fetch_array($result2, MYSQL_ASSOC)) {
$row_array['ING_SW_CB'] = $row['ING_SW_CB'];
$row_array['SB_SW_CB'] = $row['SB_SW_CB'];
$row_array['NG3_SW_CB'] = $row['NG3_SW_CB'];
$row_array['Mould_Close'] = $row['Mould_Close'];
$row_array['Leak_Test'] = $row['Leak_Test'];
$row_array['ML_Load'] = $row['ML_Load'];
$row_array['Pre_Heat'] = $row['Pre_Heat'];
$row_array['Dispense'] = $row['Dispense'];
$row_array['A310'] = $row['A310'];
$row_array['Gelation'] = $row['Gelation'];
$row_array['Platen'] = $row['Platen'];
$row_array['Mainline_Unload'] = $row['Mainline_Unload'];
$row_array['De_mould'] = $row['De_mould'];
$row_array['Clean_Up'] = $row['Clean_Up'];
$row_array['Soda_Blast'] = $row['Soda_Blast'];
$row_array['Miscellaneous'] = $row['Miscellaneous'];
//push the values in the array
array_push($json_response,$row_array);
}
$json_data = json_encode($json_response, JSON_PRETTY_PRINT);
file_put_contents('your_json_file.json', $json_data);
I need the keywords "key" & "value" added to the output so I can popuate a D3 chart.
You need to loop on the row's results and build more arrays:
while(...) {
$temp = array();
foreach($row as $key => $value) {
$temp[] = array('key' => $key, 'value' => $value);
}
$json_response[] = $temp;
}
Loop over the columns:
$json_response = array();
while ($row = mysqli_fetch_array($result2, MYSQLI_ASSOC)) {
$json_row = array();
foreach ($row as $key => $value) {
$json_row[] = array('key' => $key, 'value' => $value);
}
$json_response[] = $json_row;
}
$json_data = json_encode($json_response, JSON_PRETTY_PRINT);
file_put_contents('your_json_file.json', $json_data);
$json_response = array();
while ($row = mysqli_fetch_array($result2, MYSQL_ASSOC)) {
//push the values in the array
foreach($row as $k=>$v){
$my_array = array("key"=>$k, "value"=>$v);
}
array_push($json_response,$my_array);
}
$json_data = json_encode($json_response, JSON_PRETTY_PRINT);
file_put_contents('your_json_file.json', $json_data);
Related
i have a array result like this
{"data":{"NamaKecamatan":"BEJI","total":"2","laki":"1","cewe":"1"}}
{"data":{"NamaKecamatan":"BOJONGSARI","total":0,"laki":0,"cewe":0}}
{"data":{"NamaKecamatan":"CILODONG","total":"2","laki":"1","cewe":"1"}}
{"data":{"NamaKecamatan":"CIMANGGIS","total":"4","laki":"2","cewe":"2"}}
but i want to change array like this:
data: [{NamaKecamatan: "BEJI", total: "46", laki: "18", cewe: "28"},…]
0: {NamaKecamatan: "BEJI", total: "46", laki: "18", cewe: "28"}
1: {NamaKecamatan: "BOJONGSARI", total: "20", laki: "7", cewe: "13"}
2: {NamaKecamatan: "CILODONG", total: "93", laki: "48", cewe: "45"}
3: {NamaKecamatan: "CIMANGGIS", total: "96", laki: "47", cewe: "49"}
4: {NamaKecamatan: "CINERE", total: "13", laki: "7", cewe: "6"}]
this is my controller in php
public function countByKecamatan(){
$kecamatan = $this->db->query("SELECT IDKecamatan, NamaKecamatan FROM mskecamatan ORDER BY NamaKecamatan ASC")->result_array();
foreach ($kecamatan as $keykecamatan) {
$result = $this->MsLaporan->dataNullByKecamatan($dateStart, $dateEnd, $kategori, $keykecamatan['IDKecamatan']);
if ($result == NULL) {
$row = array(
"NamaKecamatan" => $keykecamatan['NamaKecamatan'],
"total" => 0,
"laki" => 0,
"cewe" => 0,
);
}else{
$row = $result[0];
}
$data = array(
"data" => $row,
);
echo json_encode($data);
}
}
please help me to change this data
$data = '[{"data":{"NamaKecamatan":"BEJI","total":"2","laki":"1","cewe":"1"}},
{"data":{"NamaKecamatan":"BOJONGSARI","total":0,"laki":0,"cewe":0}},
{"data":{"NamaKecamatan":"CILODONG","total":"2","laki":"1","cewe":"1"}},
{"data":{"NamaKecamatan":"CIMANGGIS","total":"4","laki":"2","cewe":"2"}}]';
$array_data = json_decode($data, ture);
$result = [];
foreach($array_data as $item) {
$result['data'][] = $item['data'];
}
return json_encode($result);
You can approach this as following
public function countByKecamatan(){
$kecamatan = $this->db->query("SELECT IDKecamatan, NamaKecamatan FROM mskecamatan ORDER BY NamaKecamatan ASC")->result_array();
$i = 0;
foreach ($kecamatan as $keykecamatan) {
$result = $this->MsLaporan->dataNullByKecamatan($dateStart, $dateEnd, $kategori, $keykecamatan['IDKecamatan']);
if ($result == NULL) {
$row = array(
"NamaKecamatan" => $keykecamatan['NamaKecamatan'],
"total" => 0,
"laki" => 0,
"cewe" => 0,
);
}else{
$row = $result[0];
}
($i > 0) ? ($data[] = $row) : ($data = array('data' => $row));
$i++;
}
echo json_encode($data);
}
The output your getting is down to 2 minor problems in the lines...
$data = array(
"data" => $row,
);
echo json_encode($data);
The first is that you are building the array with the extra level which you say you want to remove. The second line of code then echos each piece of data one at a time. You need to build up a list of all of the records and then return the full data in one go.
public function countByKecamatan(){
// Create empty array for data
$data = [];
$kecamatan = $this->db->query("SELECT IDKecamatan, NamaKecamatan FROM mskecamatan ORDER BY NamaKecamatan ASC")->result_array();
foreach ($kecamatan as $keykecamatan) {
$result = $this->MsLaporan->dataNullByKecamatan($dateStart, $dateEnd, $kategori, $keykecamatan['IDKecamatan']);
if ($result == NULL) {
$row = array(
"NamaKecamatan" => $keykecamatan['NamaKecamatan'],
"total" => 0,
"laki" => 0,
"cewe" => 0,
);
}else{
$row = $result[0];
}
// Add new row of data to data to be returned
$data[] = $row;
}
// Return all of the data in JSON format
return json_encode($data);
}
Note that this returns the value rather than echos it - you may need to change this if when you call the controller, your framework needs to encode it some other way (i.e. as part of a Response object).
As you can see in the JSON file, this looks not so necessary.
I get values from input-s and add it to the array and send it via AJAX. A simple array I know how to convert, but a multidimensional one is not. Can eat what that function? I tried to create an array with "keys", but there is a lot of trouble, I never reached the end , and I'm sure it's not right. Tell me what you can do.
i want this
{
"user1" : {
first_name":"Michael",
"last_name":"Podlevskykh",
"phones" : {
"phone_1":"5345",
"phone_2":"345345",
"phone_3":"123"
}
}
}
//this is what i see
JSON
[
{"first_name":"Michael"},
{"last_name":"Podlevskykh"},
[{"phone_1":"5345"},
{"phone_2":"345345"},
{"phone_3":"0991215078"}
]
]
PHP
//[["5345", "345345", "123"], "Michael", "Podlevskykh"]
$userInfo = (json_decode($_POST["phones"], true));
$namePhones = ["phone_1", "phone_2", "phone_3"];
$nameUser = ["first_name", "last_name"];
$jsonPhones = $userInfo;
$nameLName = $userInfo;
$jsonPhones = array_splice($jsonPhones, 0, 1);
$nameLName = array_splice($nameLName, -2);
foreach ($jsonPhones[0] as $key => $value) {
$phones[] = array($namePhones[$key] => $jsonPhones[0][$key]);
}
foreach ($nameLName as $key => $value) {
$usersName[] = array($nameUser[$key] => $nameLName[$key]);
}
array_push($usersName, $phones);
echo "<pre>";
echo json_encode($usersName);
//[
// {"first_name":"Michael"},{"last_name":"Podlevskykh"},
// [{"phone_1":"5345"},{"phone_2":"345345"},{"phone_3":"123"}]
//]
I don't get all the complications, I would do something like this if I'm sure the $input format is the same:
<?php
$input = '[["5345", "345345", "123"], "Michael", "Podlevskykh"]';
$input = json_decode($input, true);
$output = [
'user1' => [
'first_name' => $input[1],
'last_name' => $input[2],
'phones' => [
'phone_1' => $input[0][0],
'phone_2' => $input[0][1],
'phone_3' => $input[0][2]
]
]
];
echo '<pre>';
echo json_encode($output);
If you want an object as output, you need to create an object:
$userInfo = (json_decode($_POST["phones"], true));
$namePhones = ["phone_1", "phone_2", "phone_3"];
$nameUser = ["first_name", "last_name"];
$jsonPhones = $userInfo;
$nameLName = $userInfo;
$jsonPhones = array_splice($jsonPhones, 0, 1);
$nameLName = array_splice($nameLName, -2);
$user = new stdClass();
foreach ($nameLName as $key => $value) {
$user->{$nameUser[$key]} = $nameLName[$key];
}
$phones = new stdClass();
foreach ($jsonPhones[0] as $key => $value) {
$phones->{$namePhones[$key]} = $jsonPhones[0][$key];
}
$user->phones = $phones;
$users = new stdClass();
$users->user1 = $user;
echo json_encode($users);
Output:
{"user1": {
"first_name":"Michael",
"last_name":"Podlevskykh",
"phones":{
"phone_1":"5345",
"phone_2":"345345",
"phone_3":"123"
}
}
}
I want to save key and value of an array inside a foreach loop in a database with mssql and php.
Here is my array, i encoded it on json ($myArr):
{
"UPC-A": "55055",
"EAN-13": "7077707",
"UPC": "0940",
"GTIN": "009642",
"GTIN-14": "566642"
}
This is my code:
foreach($myArr as $key => $val){
$insert = "Insert Into tbl (barcodeType,barcode) Values ($key, $val)"
$stmtBar = $db->prepare($insertBarcode);
$stmtBar->execute();
//what is the best approach for this???
}
The output should be like this when encode in json from database:
{
"barcodeType": "UPC-A",
"barcode": "55055"
},
{
"barcodeType": "EAN-13",
"barcode": "7077707"
}
try use this -
<?php
$a = '{
"UPC-A": "55055",
"EAN-13": "7077707",
"UPC": "0940",
"GTIN": "009642",
"GTIN-14": "566642"
}';
$myArr = json_decode($a);
$values = '';
foreach($myArr as $key => $val){
$values .= "(".$key.",".$val."),";
}
$insert = "Insert Into tbl (barcodeType,barcode) Values ".rtrim($values,',');
$stmtBar = $db->prepare($insert);
$stmtBar->execute();
?>
Try this
<?php
$jsonData = '{
"UPC-A": "55055",
"EAN-13": "7077707",
"UPC": "0940",
"GTIN": "009642",
"GTIN-14": "566642"
}';
$myArr = json_decode($jsonData);
foreach($myArr as $key => $val){
$insert = "Insert Into tbl (barcodeType,barcode) Values ($key, $val)"
$stmtBar = $db->prepare($insertBarcode);
$stmtBar->execute();
}
$selectStmt = "select * from tbl";
$jsonArray = array();
foreach ($db->query($selectStmt) as $results)
{
$jsonArray[] = array('barcodeType' => $results['barcodeType'],'barcode' => $results['barcode']);
}
$result = json_encode($jsonArray,true);
var_dump($result);
?>
How can I replace the locations here with a foreach loop? When I put the foreach inside of the array, the code breaks. I guessing the answer has to do with creating a foreach variable and then entering that for the levels value instead?
This is my array
$data = array(
'name' => Locations,
'data' => '{
"title":"USA",
"location":"World",
"levels":[
{
"id":"states",
"title":"States",
"locations":[
{
"id":"ca",
"title":"California",
"pin":"hidden",
"x":"0.0718",
"y":"0.4546",
},
{
"id":"wa",
"title":"Washington",
"pin":"hidden",
"x":"0.1331",
"y":"0.0971"
}
]
}
]
}'
Here is my current foreach.
foreach ($response->records as $record) {
$id = $record->Id;
$title = $record->Title;
$pin = $record->Pin;
$x = $record->X;
$y = $record->Y;
echo {
"id": $id,
"title": $title,
"pin": $pin,
"x": $x,
"y": $y
}
}
Any help would be appreciated.
$arr = array();
foreach ($response->records as $record) {
$r['id'] = $record->Id;
$r['title'] = $record->Title;
$r['pin'] = $record->Pin;
$r['x'] = $record->X;
$r['y'] = $record->Y;
$arr[] = $r;
}
echo json_encode($arr);
I am trying to create a json using php
while ($info = mysqli_fetch_array($result,MYSQLI_ASSOC))
{
$id = stripslashes($info['id']);
$pricedol = stripslashes($info['pricedol']);
$final_result = array('id' => $id,'pricedol' => $pricedol );
}
$json = json_encode($final_result);
echo $json;
This is giving the following output
{
"id": 14567,
"pricedol": 15.57
}
But i have couple of records in the db...i want the following output
{
"id": 14567,
"pricedol": 15.57
},
{
"id": 4567,
"pricedol": 55.25
},
One more thing...do i need to serialize before parsing in jquery
You could create a multi-dimensional array and use json_encode on that array:
$final_result = array();
while ($info = mysqli_fetch_array($result,MYSQLI_ASSOC))
{
$id = stripslashes($info['id']);
$pricedol = stripslashes($info['pricedol']);
$final_result[] = array('id' => $id,'pricedol' => $pricedol);
}
$json = json_encode($final_result);
echo $json;