i want to use Cal-Heatmap to visualize data and my current sql result is returned as
[ { "dateStampSecs": "1499637600", "value": "1" } ]
but i want
[ { "1499637600" : 1 } ]
How do I stick them together?
I am using Codeigniter and below is my sql:
public function get_calData(){
$this->db->select('unix_timestamp(STR_TO_DATE(date_start, "%Y-%m-%d")) dateStampSecs,count(*) as value');
$this->db->from('calendar');
$query = $this->db->get()->result();
print json_encode($query,JSON_PRETTY_PRINT);
}
$dateStampSecs = array_column($query, "dateStampSecs");
$value = array_column($query, "value");
$result = array_combine($dateStampSecs, $value);
print json_encode($result,JSON_PRETTY_PRINT);
you have to echo the result like following
echo json_encode($query);
exit();
Try like this
$finalData='';
foreach ($query as $data){
$finalData[][$data['dateStampSecs']]=$data['value'];
}
$finalData=json_encode($finalData);
dd($query,$finalData);
It will give output as
Hope it will work
$query = json_decode($query ,true);
$query = [$query['dateStampSecs']=>$query['value']];
dd(json_encode($query));`
Related
I need to have data output in a very specific format to be used with something.
$product = Product::with('product_attribute_values', 'product_attribute_values.product_attribute_key')->find(2);
$product_decoded = json_decode($product, true);
I need to extract product attribute values into a specific format and it currently looks like:
I wish for it be like:
{
"Material":"Plastic",
"Printing Method":"Pad",
"Ink Colour":"Black",
"Barrel Colour":"Silver",
"Grip Colour":"Black"
}
I have attempted this:
$final_array = array();
foreach($product_decoded['product_attribute_values'] as $pav) {
$array = [
$pav['product_attribute_key']['name'] => $pav['name']
];
array_push($final_array, $array);
}
return json_encode($final_array);
This results in data looking like:
[
{"Material":"Plastic"},
{"Printing Method":"Pad"},
{"Ink Colour":"Black"},
{"Barrel Colour":"Silver"},
{"Grip Colour":"Black"}
]
How would this be achieved?
You can do it like this:
foreach($product_decoded['product_attribute_values'] as $pav) {
$array[$pav['product_attribute_key']['name']] = $pav['name'];
}
return json_encode($array);
For something like this you could use collections:
return collect($product_decoded['product_attribute_values'])
->pluck('name', 'product_attribute_key.name')
->toJson();
Alternatively, you could use the array helpers:
$finalArray = Arr::pluck($product_decoded['product_attribute_values'],'name', 'product_attribute_key.name' );
return json_encode($finalArray);
You need to have it in an object instead of an array.
$item = array("Material"=>"Plastic", "Printing Method"=>"Pad", "Ink Colour"=>"Black", "Barrel Colour"=>"Silver", "Grip Colour"=>"Black");
echo json_encode($item);
will result in this JSON:
[
{"Material":"Plastic"},
{"Printing Method":"Pad"},
{"Ink Colour":"Black"},
{"Barrel Colour":"Silver"},
{"Grip Colour":"Black"}
]
Which is correct JSON array syntax.
In order to get
{
"Material":"Plastic",
"Printing Method":"Pad",
"Ink Colour":"Black",
"Barrel Colour":"Silver",
"Grip Colour":"Black"
}
You would need soemthing like
$item->Material = "Plastic";
$item->PrintingMethod = "Pad";
$item->InkColour = "Black";
$item->Barrel Colour = "Silver";
$item->GripColour = "Black;
json_encode($item);
I am new to PHP so far I managed to get the following output from database in JSON format.I created an array result which returns workorderName, but I want to get workOrderId also in the same array so that I can use it in android string request.
{
"result": [
{
"$workOrderName": "electrician"
},
{
"$workOrderName": "plumber"
},
{
"$workOrderName": "carpenter"
}
]
}
my php code is
<?PHP
require_once('connection.php');
$workName = "SELECT work_order_id,workorder_name FROM workorder_category";
$con=mysqli_connect($server_name,$user_name,$password,$db);
$r = mysqli_query($con,$workName);
$result = array();
$resultArr = array('success' => true);
while($row = mysqli_fetch_array($r)){
array_push($result,array('$workOrderName'=>$row['workorder_name']));
}
echo json_encode(array('result'=>$result));
mysqli_close($con);
?>
I want the Output like
{
"result": [
{
"$workOrderId":"1"
"$workOrderName": "electrician"
},
{
"$workOrderId":"2"
"$workOrderName": "plumber"
},
{
"$workOrderId":"3"
"$workOrderName": "carpenter"
}
]
}
I'm pretty sure you didn't mean to have the $ in the key, but this is easier and will give you the column names (work_order_id, workorder_name) from the table as keys. Make sure to use mysqli_fetch_assoc:
while($row = mysqli_fetch_assoc($r)){
$result[] = $row;
}
If you want to change the keys then you can alias them in the query and use the above:
$workName = "SELECT work_order_id AS workOrderID, workorder_name AS workOrderName FROM workorder_category";
Or you could build the array:
$result[] = array('workOrderID' => $row['work_order_id'],
'workOrderName' => $row['workorder_name']);
You need to tweak your code to add $workOrderId also in the array like below
array_push($result,array(
'$workOrderId' => $row['work_order_id']
'$workOrderName' => $row['workorder_name']
));
I need to convert the below result. I generate this JSON array using PHP but now I need the same result into a single curly brace. Below is my PHP code to generate the JSON.
$arr = array();
#If no data was returned, check for any SQL errors
if ($res == false)
{
echo 'Query not Executed : '. die(print_r(sqlsrv_errors(), TRUE));
}
else
{
while($obj = sqlsrv_fetch_array($res, SQLSRV_FETCH_ASSOC))
{
$arr[] = array(
'code' => array_search($obj['name'], $countries_iso),
'total' => $obj['Total']
);
}
}
header("Content-type: application/json");
#Output the JSON data
print (json_encode($arr));
Output of above PHP code:
[
{
code: "AF",
total: 1
},
{
code: "DZ",
total: 1
},
{
code: "AS",
total: 2
}
]
But I want to show like below result:
{
"AF":1
"DZ": 1,
"AS": 2
}
I guess you want $arr[array_search($obj['name'], $countries_iso)] = $obj['Total'];
Your mistake was the fact that you were declaring a new array each time you add something to your original array. This way you assign the correct way :)
Try this code:
$arr = array();
#If no data was returned, check for any SQL errors
if ($res == false)
{
echo 'Query not Executed : '. die(print_r(sqlsrv_errors(), TRUE));
}
else
{
while($obj = sqlsrv_fetch_array($res, SQLSRV_FETCH_ASSOC))
{
$arr[array_search($obj['name'], $countries_iso)] = $obj['Total'];
}
}
header("Content-type: application/json");
#Output the JSON data
//$json = json_encode($arr);
$json = json_encode($arr);
Just do this:
$arr[array_search($obj['name'], $countries_iso)] = $obj['Total'];
But you can get rid of the whole array_search($obj['name'], $countries_iso) if you restrict the query to only return rows WHERE name IN (list of $countries_iso). Look up WHERE and IN for SQL Server.
You need to transform your data structure to an associative array, where the value you are currently storing in code will be the key and the value stored in total will be value.
I'll be able to help better if you post the code that generates your current JSON object
Hi I have some table say fro example table1 ,table2 in my mysql databse.
I need to get following json result from php.
Can anyone suggest how to achieve this?
any good tutorial doing same is also helpful.
I am able to convert database result in simple json response but custom response is something difficult for me.
{
response:ok
tables:[
{
name:table name
data:[
{
fieldname1:value1
fieldname2:values2
},
{
fieldname1:value1
fieldname2:value2
}
.
.
]
},
{
name:table name1
data:[
{
fieldname1:value1
fieldname2:values2
},
{
fieldname1:value1
fieldname2:value2
}
.
.
]
},
]
}
}
Quoting from How to convert mysql data base table data in json using php, once you have your table names you can do for each of them.
$result = array();
$result['response'] = 'ok'
foreach ($tables as $tableName) {
$query = mysql_query("SELECT * FROM $tableName");
$rows = array();
while($row = mysql_fetch_assoc($query)) {
$rows[] = $row;
}
$result['tables'][] = array(
'name' = $tableName,
'data' = $rows
)
}
print json_encode($result);
Basically idea is to grab data from MySQL table and transform it to JSON.
This is how database table looks:
And this how should output be:
[
{"group1":[
{"val":"somevalue"},
{"val":"somevalue"}
]
},
{"group2":[
{"val":"somevalue"},
{"val":"somevalue"}
]
},
{"group3":[
{"val":"somevalue"}
]
}
]
My PHP script looks like this, for now:
$arr = [];
$result = mysql_query("SELECT * FROM thetable WHERE section='sect1'");
while($row = mysql_fetch_array($result))
{
// ???
}
echo json_encode($arr);
My main issue is how to output/sort data in "groups".
Thanks for your help!
try this
while($row = mysql_fetch_array($result))
{
$arr[$row['group']][] = array('val' => $row['value']);
}