Related
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:
This question already has answers here:
PHP Array to JSON Array using json_encode(); [duplicate]
(4 answers)
Closed 5 years ago.
I tried real hard for my title to make sense haha. I have this JSON:
[{
"0": {
"id": 130427,
"created_at": 1512521776301,
"updated_at": 1512549188911,
"category": 0,
"platform": 6,
"date": 1513987200000,
"region": 8,
"y": 2017,
"m": 12,
"human": "2017-Dec-23",
"game": 76663
},
"2": {
"id": 131795,
"created_at": 1514172411633,
"updated_at": 1514190849639,
"category": 0,
"platform": 39,
"date": 1513987200000,
"region": 8,
"y": 2017,
"m": 12,
"human": "2017-Dec-23",
"game": 78658
}
}]
As you can see the position of the JSON object in the global json serves as the name of the object and I don't want this. This is what I want:
[{
"id": 130427,
"created_at": 1512521776301,
"updated_at": 1512549188911,
"category": 0,
"platform": 6,
"date": 1513987200000,
"region": 8,
"y": 2017,
"m": 12,
"human": "2017-Dec-23",
"game": 76663
},
{
"id": 131795,
"created_at": 1514172411633,
"updated_at": 1514190849639,
"category": 0,
"platform": 39,
"date": 1513987200000,
"region": 8,
"y": 2017,
"m": 12,
"human": "2017-Dec-23",
"game": 78658
}
]
I want the objects without a name. This is the code I'm using:
$json = file_get_contents('./releases.json');
$data = json_decode($json, TRUE);
$region = isset($_GET['region']) ? $_GET['region'] : null;
# if region is not null: ?region=8
if ($region) {
$region_filter = function($v) use ($region) {
// 8 == Worldwide
if ($v['region'] == $region || $v['region'] == 8) {
return true;
} else {
return false;
}
};
$data = array_filter($data['data'], $region_filter);
}
header('Content-Type: application/json');
echo json_encode(array($data)); // good
Thank you
You need to use array_values() to reindex the array.
PHP's json_encode() function will only produce an array if all array keys are numeric and don't have any gaps, e.g. 0, 1, 2, 3 etc. The problem is that array_filter() can remove certain keys and leave gaps, which causes json_encode() to include the keys as you show in your example. You can fix this by using array_values() to re-index the array before calling json_encode().
Here is an example:
<?php
// numeric array keys with no gaps
$a = ['a', 'b', 'c'];
echo json_encode($a);
// ["a","b","c"]
// filter out the 'b' element to introduce a gap in the keys
$a = array_filter($a, function ($v) {
return $v !== 'b';
});
echo json_encode($a);
// {"0":"a","2":"c"}
// re-index the array to remove gaps
$a = array_values($a);
echo json_encode($a);
// ["a","c"]
I have design a simple graph with chart.js, Which looking cool.
But now i want to show their data between one Month to another Month.
Means their are following data:
1th january, 2017 : 12
3rd January, 2017: 16
And so on..
now i want to show their January to February data as image,which is shown below:
Here is my simple code
var ctx = document.getElementById('myChart').getContext('2d');
var chart = new Chart(ctx, {
// The type of chart we want to create
type: 'line',
// The data for our dataset
data: {
labels: ["January 2017", "February 2017", "March 2017", "April 2017", "May 2017", "June 2017", "July 2017"],
datasets: [{
label: "My First dataset",
backgroundColor: 'rgb(255, 99, 132,0.25)',
borderColor: 'rgb(255, 99, 132)',
data: [0, 10, 5, 2, 20, 30, 15],
lineTension:0
}]
},
// Configuration options go here
options: {}
});
jsfiddle Here : https://jsfiddle.net/2qxj9t00/2/
I've google so many times but no any solution are their, so kindly suggest me. Thanks :)
You can accomplish this using the following x-axis ticks callback function ...
ticks: {
callback: function(e) {
var tick = e.match(/(Jan|Feb|Mar|Apr|May|Jun|Jul|Aug|Sept|Oct|Nov|Dec).*?\d+/g)[0];
if (tick != aR) {
aR = tick;
return tick;
}
}
}
assuming your labels array would look somewhat like this.. ["1st January 2017", "3rd January 2017", "4th February 2017", ...]
ᴡᴏʀᴋɪɴɢ ᴇxᴀᴍᴘʟᴇ
var aR = null; //store already returned tick
var ctx = document.getElementById('myChart').getContext('2d');
var chart = new Chart(ctx, {
type: 'line',
data: {
labels: ["1st January 2017", "3rd January 2017", "4th February 2017", "9th February 2017", "3rd March 2017", "15th March 2017"],
datasets: [{
label: "My First dataset",
backgroundColor: 'rgba(255, 99, 132, 0.5)',
borderColor: 'rgb(255, 99, 132)',
data: [12, 16, 10, 11, 9, 15],
lineTension: 0
}]
},
options: {
scales: {
xAxes: [{
ticks: {
autoSkip: false,
callback: function(e) {
var tick = e.match(/(Jan|Feb|Mar|Apr|May|Jun|Jul|Aug|Sept|Oct|Nov|Dec).*?\d+/g)[0];
if (tick != aR) {
aR = tick;
return tick;
}
}
}
}],
yAxes: [{
ticks: {
min: 0,
max: 30
}
}]
}
}
});
<script src="https://cdnjs.cloudflare.com/ajax/libs/Chart.js/2.6.0/Chart.min.js"></script>
<canvas id="myChart" height="200"></canvas>
I am trying to have my line chart to show the result within tooltip as a string format (e.g: Jun 28, 2015 - Jul 4, 2015) while having my hAxis to display its values as date format (e.g: Jun 29).
Similar as Analytics:
However when I draw the chart with the returned Json data shown below:
{
"cols": [{
"id": "A",
"label": "Date Range",
"pattern": "",
"type": "string"
}, {
"id": "B",
"label": "Sessions",
"pattern": "",
"type": "number"
}, {
"id": "C",
"label": "Pageviews",
"pattern": "",
"type": "number"
}],
"rows": [{
"c": [{
"v": "Date(2015,5,23)",
"f": "Jun 23, 2015 - Jun 27, 2015"
}, {
"v": 1645
}, {
"v": 5237
}]
}, {
"c": [{
"v": "Date(2015,5,28)",
"f": "Jun 28, 2015 - Jul 04, 2015"
}, {
"v": 2189
}, {
"v": 6977
}]
}, {
"c": [{
"v": "Date(2015,6,05)",
"f": "Jul 05, 2015 - Jul 11, 2015"
}, {
"v": 2168
}, {
"v": 6862
}]
}, {
"c": [{
"v": "Date(2015,6,12)",
"f": "Jul 12, 2015 - Jul 18, 2015"
}, {
"v": 1661
}, {
"v": 5735
}]
}, {
"c": [{
"v": "Date(2015,6,19)",
"f": "Jul 19, 2015 - Jul 23, 2015"
}, {
"v": 1109
}, {
"v": 3826
}]
}]
}
My chart is showing the hAxis with f property's value instead of v property's value as per shown below:
The data type for the hAxis is set as string.
With the information given, may I ask how I can achieve my desired results?
Have you tried setting the 'options' -> 'vAxis' -> 'ticks' parameter?
Then your json data would look something like this:
{
"cols": [your columns],
"rows": [your rows],
"options": {
"vaxis": {
"ticks": ["jun 29", "jul 6", "jul 13"]
}
}
}
Here's a link to google charts documentation on line charts, I hoe this helps :-)
Edit:
Here's a working jsfiddle. I don't know what method you are using to load your data but this worked for me.
For some reason, the labels were displayed correctly after I set the width and height parameters in options.
Based on the working jsfiddle provided by #bjarkig82. I have figured on how to fix the issue and achieve my objective.
First, the data type for column date needs to be changed from string to date
var cols = [{
"id": "A",
"label": "Date Range",
"pattern": "",
"type": "date"
}, {
"id": "B",
"label": "Sessions",
"pattern": "",
"type": "number"
}, {
"id": "C",
"label": "Pageviews",
"pattern": "",
"type": "number"
}];
var rows = [{
"c": [{
"v": "Date(2015,5,23)",
"f": "Jun 23, 2015 - Jun 27, 2015"
}, {
"v": 1645
}, {
"v": 5237
}]
}, {
"c": [{
"v": "Date(2015,5,28)",
"f": "Jun 28, 2015 - Jul 04, 2015"
}, {
"v": 2189
}, {
"v": 6977
}]
}, {
"c": [{
"v": "Date(2015,6,05)",
"f": "Jul 05, 2015 - Jul 11, 2015"
}, {
"v": 2168
}, {
"v": 6862
}]
}, {
"c": [{
"v": "Date(2015,6,12)",
"f": "Jul 12, 2015 - Jul 18, 2015"
}, {
"v": 1661
}, {
"v": 5735
}]
}, {
"c": [{
"v": "Date(2015,6,19)",
"f": "Jul 19, 2015 - Jul 23, 2015"
}, {
"v": 1109
}, {
"v": 3826
}]
}];
Secondly, I comment the section of code shown below or it will cause my string date (e.g: Jun 28, 2015 - Jul 4, 2015) to show as date (e.g: Jun 28, 2015) within the tooltip instead.
var formatter = new google.visualization.DateFormat({ pattern: "EEEE, MMM dd, yyyy" });
formatter.format(data, 0);
To understand better, please check out this jsfiddle
I have the following data
{
total: "156",
list: [
{
"nodeRef": "workspace://SpacesStore/e364714d-14bc-4e13-bfff-c1f86a8cbe67",
"id": "e364714d-14bc-4e13-bfff-c1f86a8cbe67",
"name": "Morning Class_Dadi Janki_29-05-12_H_London.mp4",
"mimetype": "video/mp4",
"title" : "Morning Class" ,
"author": "Dadi Janki",
"class_date": "May 29, 2012 12:00:00 AM",
"created": "May 29, 2012 12:32:44 PM",
"size": "97,156,420",
"lang": "h",
"totalViews": "11",
"totalDownloads": "0",
"downloadUrl": "/d/a/workspace/SpacesStore/e364714d-14bc-4e13-bfff-c1f86a8cbe67/Morning%20Class_Dadi%20Janki_29-05-12_H_London.mp4"
}
]
}
When I try to var_dump it
It gives me null. How do I know whether the data is JSON encoded or not ?
Edit:
Here's the code
I get the above content by get_contents to the url
$url = ""; // URL
$contents = file_get_contents($url);
$data = json_decode($contents);
var_dump($data);
<?php
$str = '{
total: "156",
list: [
{
"nodeRef": "workspace://SpacesStore/e364714d-14bc-4e13-bfff-c1f86a8cbe67",
"id": "e364714d-14bc-4e13-bfff-c1f86a8cbe67",
"name": "Morning Class_Dadi Janki_29-05-12_H_London.mp4",
"mimetype": "video/mp4",
"title" : "Morning Class" ,
"author": "Dadi Janki",
"class_date": "May 29, 2012 12:00:00 AM",
"created": "May 29, 2012 12:32:44 PM",
"size": "97,156,420",
"lang": "h",
"totalViews": "11",
"totalDownloads": "0",
"downloadUrl": "/d/a/workspace/SpacesStore/e364714d-14bc-4e13-bfff-c1f86a8cbe67/Morning%20Class_Dadi%20Janki_29-05-12_H_London.mp4"
}
]
}
';
$str = preg_replace('#([^\s\"]+): #is', '"\\1": ', $str);
echo $str;
?>
First whenever you will get any json value or you are sending any value in json format first check whether Json is valid or not..
Json validator and formator:-
http://jsonformatter.curiousconcept.com/
Json validator:-
http://www.jsonlint.org/
Then in Your code try to find error :- json_last_error
And try to use Json encode and Decode for Url values.. that will be helpful always..
Valid Json:-
{
"total":"156",
"list":[
{
"nodeRef":"workspace://SpacesStore/e364714d-14bc-4e13-bfff-c1f86a8cbe67",
"id":"e364714d-14bc-4e13-bfff-c1f86a8cbe67",
"name":"Morning Class_Dadi Janki_29-05-12_H_London.mp4",
"mimetype":"video/mp4",
"title":"Morning Class",
"author":"Dadi Janki",
"class_date":"May 29, 2012 12:00:00 AM",
"created":"May 29, 2012 12:32:44 PM",
"size":"97,156,420",
"lang":"h",
"totalViews":"11",
"totalDownloads":"0",
"downloadUrl":"/d/a/workspace/SpacesStore/e364714d-14bc-4e13-bfff-c1f86a8cbe67/Morning%20Class_Dadi%20Janki_29-05-12_H_London.mp4"
}
]
}
Json:--
{"total":"156","list":[{"nodeRef":"workspace://SpacesStore/e364714d-14bc-4e13-bfff-c1f86a8cbe67","id":"e364714d-14bc-4e13-bfff-c1f86a8cbe67","name":"Morning Class_Dadi Janki_29-05-12_H_London.mp4","mimetype":"video/mp4","title":"Morning Class","author":"Dadi Janki","class_date":"May 29, 2012 12:00:00 AM","created":"May 29, 2012 12:32:44 PM","size":"97,156,420","lang":"h","totalViews":"11","totalDownloads":"0","downloadUrl":"/d/a/workspace/SpacesStore/e364714d-14bc-4e13-bfff-c1f86a8cbe67/Morning%20Class_Dadi%20Janki_29-05-12_H_London.mp4"}]}