This is the static multidimension array and i converted to json.
$mailmsg = array(
'type'=> 'line',
'data' => array(
'labels' => array("1","2","3","4","5"),
'datasets' => array(array("label"=>"A","fill"=>"false","yAxisID"=>"A","borderColor"=>"#bae755","data"=>array(100, 96, 84, 76, 69)),array("label"=>"B","fill"=>"false","yAxisID"=>"B","borderColor"=>"#55bae7","data"=>array(1, 1, 1, 1, 0)),array("label"=>"C","fill"=>"false","yAxisID"=>"C","borderColor"=>"#e755ba","data"=>array(5, 15, 10, 10, 0)))),
' '=>array("scales"=>array("yAxes"=>array(array("scaleLabel"=>array("display"=>"true","labelString"=>"Prn1"),"id"=>"A","type"=>"linear","position"=>"left"),array("scaleLabel"=>array("display"=>"true","labelString"=>"Prn2"),"id"=>"B","type"=>"linear","position"=>"left"),array("scaleLabel"=>array("display"=>"true","labelString"=>"Prn3"),"id"=>"C","type"=>"linear","position"=>"left")))
));
echo $json = json_encode((object)$mailmsg, JSON_NUMERIC_CHECK);
Result:
{
"type": "line",
"data": {
"labels": ["1","2","3","4","5"],
"datasets": [{
"label": "A",
"fill": "false",
"yAxisID": "A",
"borderColor": "#bae755",
"data": [100, 96, 84, 76, 69]
}, {
"label": "B",
"fill": "false",
"yAxisID": "B",
"borderColor": "#55bae7",
"data": [1, 1, 1, 1, 0]
}, {
"label": "C",
"fill": "false",
"yAxisID": "C",
"borderColor": "#e755ba",
"data": [5, 15, 10, 10, 0]
}]
},
"options": {
"scales": {
"yAxes": [{
"scaleLabel": {
"display": "true",
"labelString": "Prn1"
},
"id": "A",
"type": "linear",
"position": "left"
}, {
"scaleLabel": {
"display": "true",
"labelString": "Prn2"
},
"id": "B",
"type": "linear",
"position": "left"
}, {
"scaleLabel": {
"display": "true",
"labelString": "Prn3"
},
"id": "C",
"type": "linear",
"position": "left"
}]
}
}
}
I need to above static array to change dynamically using foreach.
just pass the reference to the specific data in the array:
foreach($mailmsg['data']['datasets'] as $key => $value){
}
Related
Let's consider the following JSON response. It is a list of station objects.
[
{
"id": 43,
"trucks": [
{
"id": 24,
"vehicleType": {
"id": 2,
"vehicleType": "VT",
"createdAt": 1622645476000,
"updatedAt": 1622645476000
},
"sensor": {
"id": 16,
"devId": "tutorial",
"readings": [
{
"id": 36296,
"interiorTemperature": 528.36,
"batteryTemperature": 528.36,
"batteryVoltage": 0,
"createdAt": 1623065279000,
"updatedAt": 1623065279000
}
],
"resolved": false,
"createdAt": 1622645598000,
"updatedAt": 1622650553000
},
"vehicleNumber": 123456,
"truckStatus": true,
"defaultBatteryVoltage": "12",
"batteryTempThreshold": 25,
"interiorTempThresholdMax": 30,
"interiorTempThresholdMin": 5,
"batteryVoltageThreshold": 11.5,
"createdAt": 1622645517000,
"updatedAt": 1622645663000
},
{
"id": 29,
"vehicleType": {
"id": 2,
"vehicleType": "VT",
"createdAt": 1622645476000,
"updatedAt": 1622645476000
},
"sensor": {
"id": 23,
"devId": "value",
"readings": [
{
"id": 36298,
"interiorTemperature": 10,
"batteryTemperature": 10,
"batteryVoltage": 10,
"createdAt": 1623074142000,
"updatedAt": 1623074142000
}
],
"resolved": false,
"createdAt": 1623074089000,
"updatedAt": 1623074089000
},
"vehicleNumber": 313215,
"truckStatus": true,
"defaultBatteryVoltage": "12",
"batteryTempThreshold": 50,
"interiorTempThresholdMax": 50,
"interiorTempThresholdMin": 50,
"batteryVoltageThreshold": 50,
"createdAt": 1623073365000,
"updatedAt": 1623073365000
}
],
"name": "value",
"city": "value",
"address": "value",
"zipCode": "value",
"createdAt": 1622645352000,
"updatedAt": 1622645352000
},
{
"id": 44,
"trucks": [
{
"id": 25,
"vehicleType": {
"id": 3,
"vehicleType": "AG",
"createdAt": 1622647436000,
"updatedAt": 1622647436000
},
"sensor": {
"id": 18,
"devId": "test",
"readings": [
null
],
"resolved": false,
"createdAt": 1622795394000,
"updatedAt": 1622795394000
},
"vehicleNumber": 123457,
"truckStatus": true,
"defaultBatteryVoltage": "24",
"batteryTempThreshold": 80,
"interiorTempThresholdMax": 50,
"interiorTempThresholdMin": -20,
"batteryVoltageThreshold": 11.5,
"createdAt": 1622645584000,
"updatedAt": 1623074846000
}
],
"name": "value",
"city": "value",
"address": "value",
"zipCode": "11112",
"createdAt": 1622645431000,
"updatedAt": 1622645431000
},
{
"id": 46,
"trucks": [
{
"id": 26,
"vehicleType": {
"id": 2,
"vehicleType": "VT",
"createdAt": 1622645476000,
"updatedAt": 1622645476000
},
"sensor": null,
"vehicleNumber": 979787,
"truckStatus": true,
"defaultBatteryVoltage": "12",
"batteryTempThreshold": 123,
"interiorTempThresholdMax": 123,
"interiorTempThresholdMin": 123,
"batteryVoltageThreshold": 123,
"createdAt": 1623064671000,
"updatedAt": 1623064671000
}
],
"name": "value",
"city": "value",
"address": "value",
"zipCode": "11111",
"createdAt": 1622918304000,
"updatedAt": 1622918304000
}
]
All stations have an id, a list of trucks, and some basic info (name, location etc). With PHP, I convert this to an array object. What I would like to achieve is to sort the array. Now I have managed to sort based on name or location. I used the following snipped:
usort($stations, function($a, $b) {
$retval = $a['name'] <=> $b['name'];
return $retval;
});
Now it's all nice but we would like to sort it based on the highest interior temperature read in a station. To make the station with the highest interior temperature be the 0th element in the list and the one with the lowest (or no reading at all) is the nth. I have tried playing around with usort but I had no results. Is it even possible to sort it like that?
I want to pull data from below JSON format using PHP (foreach loop).
User detail is correspondence to house detail and house detail correspondence to individual meter reading.
I want to show Username, Device_No and all meter_detail field. Please help me out.
{
"id": 7,
"Username": "user",
"Housename": "Leela",
"Houses_list": [{
"id": 1,
"Device_No": 1111,
"meter_detail": [{
"id": 1,
"flow": 12,
"date": "2020-02-13T12:05:14.709Z",
"opening": 5,
"volume": 1234
},
{
"id": 2,
"flow": 32,
"date": "2020-02-13T12:05:26.821Z",
"opening": 2,
"volume": 1235
}
]
},
{
"id": 2,
"Device_No": 231,
"meter_detail": [{
"id": 3,
"flow": 78,
"date": "2020-02-13T12:05:41.331Z",
"opening": 5,
"volume": 7854
}]
}
]
}
You need to convert the JSON to an array by using json_decode(). Then a couple of foreach loops would do work for you:
<?php
$json = '{
"id": 7,
"Username": "user",
"Housename": "Leela",
"Houses_list": [{
"id": 1,
"Device_No": 1111,
"meter_detail": [{
"id": 1,
"flow": 12,
"date": "2020-02-13T12:05:14.709Z",
"opening": 5,
"volume": 1234
},
{
"id": 2,
"flow": 32,
"date": "2020-02-13T12:05:26.821Z",
"opening": 2,
"volume": 1235
}
]
},
{
"id": 2,
"Device_No": 231,
"meter_detail": [{
"id": 3,
"flow": 78,
"date": "2020-02-13T12:05:41.331Z",
"opening": 5,
"volume": 7854
}]
}
]
}';
$input = json_decode($json, true);
echo 'Username: '.$input['Username'].'<br>';
foreach ($input['Houses_list'] as $device){
echo '<br>Device No: '.$device['Device_No'].'<br>';
foreach ($device['meter_detail'] as $metrics){
echo '<table style="border: 1px solid black">';
foreach ($metrics as $key => $metric){
echo '<tr><td>'.$key.'</td><td>'.$metric.'</td></tr>';
}
echo '</table>';
}
}
?>
Output:
I've got the following JSON array with PHP:
"data": [
{
"RecordID": 1,
"OrderID": "53150-422",
...
},
{
...
},
I do this with:
echo json_encode(array('data' => $users),JSON_PRETTY_PRINT);
Now I want to add metadata to the array like this:
{
"meta": {
"page": 1,
"pages": 1,
"perpage": -1,
"total": 350,
"sort": "asc",
"field": "RecordID"
},
"data": [
{
"RecordID": 1,
"OrderID": "53150-422",
...
},
How to to this? Thanks in advance!
write code this way
<?php
$meta=[ "page"=> 1,
"pages"=> 1,
"perpage"=> -1,
"total"=> 350,
"sort"=> "asc",
"field"=> "RecordID"];
$users= [
"RecordID"=> 1,
"OrderID"=> "53150-422"
];
echo json_encode(array('meta'=>$meta,'data' => $users),JSON_PRETTY_PRINT);
?>
I'm seeing the following error:
My params are
array (size=4)
'facets' => string 'Instock.1' (length=9)
'facetFilters' => string 'inStock.1:1' (length=11)
'numericFilters' => string 'itemId!=511283' (length=14)
'getRankingInfo' => int 1
If I remove either getRankingInfo or numericFilters, it works for all requests.
According to the logs in Algolia it's returning results, but i don't get that far on my end for some requests.
One response that causes the error to appear is:
Request
{
"params": "facets=Instock.1&facetFilters=inStock.1%3A1&numericFilters=itemId%21%3D511283&getRankingInfo=1&query=WARBURTON+ORANGE+WRAPPER+800G"
}
Response
{
"hits": [
{
"itemId": 506241,
"name": "Warburton Blue Wrapper 800g",
"slug": "506241-warburton-blue-wrapper",
"brand": "Warburton",
"supplier_name": "Warburtons Bread",
"size": "800G",
"status": "L",
"vat_rate": "0.00",
"popularity": 18992,
"image": "/images/products/506241.jpg",
"thumbnail": "/images/products/506241_thumbnail.jpg",
"barcodes": [
"5010044000039"
],
"branches": [
1,
2,
4,
5,
6
],
"deepestCategory": "07013033337",
"inStock": {
"1": 1,
"2": 0,
"4": 1,
"5": 1,
"6": 1
},
"alternatives": [
],
"objectID": "506241",
"_highlightResult": {
"name": {
"value": "<em>Warburton</em> Blue <em>Wrapper</em> <em>800g</em>",
"matchLevel": "partial",
"matchedWords": [
"warburton",
"wrapper",
"800g"
]
},
"barcodes": [
{
"value": "5010044000039",
"matchLevel": "none",
"matchedWords": [
]
}
]
},
"_rankingInfo": {
"nbTypos": 0,
"firstMatchedWord": 0,
"proximityDistance": 10,
"userScore": 21542,
"geoDistance": 0,
"geoPrecision": 1,
"nbExactWords": 3,
"words": 3
}
},
{
"itemId": 511294,
"name": "Warburton Green Wrapper ",
"slug": "511294-warburton-green-wrapper",
"brand": "Warburton",
"supplier_name": "Warburtons Bread",
"size": "",
"status": "L",
"vat_rate": "0.00",
"popularity": 78098,
"image": "/images/products/511294.jpg",
"thumbnail": "/images/products/511294_thumbnail.jpg",
"barcodes": [
"5010044002347"
],
"branches": [
1,
2,
4,
5,
6
],
"deepestCategory": "07013033337",
"inStock": {
"1":
One that works fine is:
Request body
{
"params": "facets=Instock.1&facetFilters=inStock.1%3A1&numericFilters=itemId%21%3D558471&getRankingInfo=1&query=I+CAN%27T+BELIEVE+IT%27S+NOT+BUTTER+2KG"
}
Response
{
"hits": [
{
"itemId": 581691,
"name": "I Can't Believe Its Not Butter 500g",
"slug": "581691-i-cant-believe-its-not-butter",
"brand": "I Can't Believe",
"supplier_name": "U.lever Bestfoods (chilled)",
"size": "500G",
"status": "L",
"vat_rate": "0.00",
"popularity": 36597,
"image": "/images/products/581691.jpg",
"thumbnail": "/images/products/581691_thumbnail.jpg",
"barcodes": [
"000118039904",
"05000118039904",
"5000241007009"
],
"branches": [
1,
2,
3,
4,
5,
6
],
"deepestCategory": "07015269103",
"inStock": {
"1": 1,
"2": 1,
"3": 1,
"4": 1,
"5": 1,
"6": 1
},
"alternatives": [
],
"objectID": "581691",
"_highlightResult": {
"name": {
"value": "<em>I</em> <em>Can't</em> <em>Believe</em> <em>Its</em> <em>Not</em> <em>Butter</em> 500g",
"matchLevel": "partial",
"matchedWords": [
"i",
"can",
"t",
"believe",
"it",
"s",
"not",
"butter"
]
},
"barcodes": [
{
"value": "000118039904",
"matchLevel": "none",
"matchedWords": [
]
},
{
"value": "05000118039904",
"matchLevel": "none",
"matchedWords": [
]
},
{
"value": "5000241007009",
"matchLevel": "none",
"matchedWords": [
]
}
]
},
"_rankingInfo": {
"nbTypos": 0,
"firstMatchedWord": 0,
"proximityDistance": 22,
"userScore": 15914,
"geoDistance": 0,
"geoPrecision": 1,
"nbExactWords": 6,
"words": 8
}
},
{
"itemId": 247577,
"name": "I Can't Believe Its Not Butter 250g"
It seems the result set was causing an error with json_decode. When I limited the hitsPerPage to 10 all works fine. So the answer here is to paginate large sets, obvious really.
Thanks to all who read and commented.
I am trying to get back information from a multidimensional json array but can't seem to get it right.
Below is an example of the output from the url which you can see yourself at http://109.255.189.130:3000/
{ "vnc_version": "2.1.0.0", "mod_version": "1.0.0.0", "server": { "name": "Pure Blood", "framework": "Microsoft Windows NT 6.2.9200.0" }, "stats": { "uptime": 53462.0, "uptime_peak": 53462.0, "online": 1, "online_max": 1, "online_peak": 2, "unique": 1, "unique_max": 1, "unique_peak": 2, "items": 111752, "items_max": 112259, "items_peak": 112259, "mobiles": 37963, "mobiles_max": 37976, "mobiles_peak": 37978, "guilds": 0, "guilds_max": null, "guilds_peak": 0 }, "players": [ { "info": { "id": 1, "name": "aN.Droid", "title": "", "profile": "", "guild_id": -1, "guild_abbr": "" }, "stats": [ ], "skills": [ ], "equip": [ ] } ], "guilds": [ ] }
What I would like to do is echo the name in the players array. There will be more than one player.
Can anyone please help me out here and point me in the correct direction to get this information?
I am very new to json so excuse my ignorance on the subject.
Thank you!
<?php
$json = '{ "vnc_version": "2.1.0.0", "mod_version": "1.0.0.0", "server": { "name": "Pure Blood", "framework": "Microsoft Windows NT 6.2.9200.0" }, "stats": { "uptime": 54383.3, "uptime_peak": 54383.3, "online": 1, "online_max": 1, "online_peak": 2, "unique": 1, "unique_max": 1, "unique_peak": 2, "items": 111672, "items_max": 112259, "items_peak": 112259, "mobiles": 37944, "mobiles_max": 37976, "mobiles_peak": 37978, "guilds": 0, "guilds_max": null, "guilds_peak": 0 }, "players": [ { "info": { "id": 1, "name": "aN.Droid", "title": "", "profile": "", "guild_id": -1, "guild_abbr": "" }, "stats": [ ], "skills": [ ], "equip": [ ] } ], "guilds": [ ] }';
$array = json_decode($json, true);
// you want 'true' as the second parameter so it tunrs this JSON data into a multidimensional array instead of objects.
foreach($array['players'] as $player){
print_r($player['info']);
//etc.. do what you want with each player
}
$json = '{ "vnc_version": "2.1.0.0", "mod_version": "1.0.0.0", "server": { "name": "Pure Blood", "framework": "Microsoft Windows NT 6.2.9200.0" }, "stats": { "uptime": 53462.0, "uptime_peak": 53462.0, "online": 1, "online_max": 1, "online_peak": 2, "unique": 1, "unique_max": 1, "unique_peak": 2, "items": 111752, "items_max": 112259, "items_peak": 112259, "mobiles": 37963, "mobiles_max": 37976, "mobiles_peak": 37978, "guilds": 0, "guilds_max": null, "guilds_peak": 0 }, "players": [ { "info": { "id": 1, "name": "aN.Droid", "title": "", "profile": "", "guild_id": -1, "guild_abbr": "" }, "stats": [ ], "skills": [ ], "equip": [ ] } ], "guilds": [ ] }';
$arr = json_decode($json, true);
foreach($arr["players"] as $player) print($player["info"]["name"]."<br/>");