I have php code that returns this from a database,
[
{
"Time": "2012-11-27 16:10:35",
"NumPlayers": "1"
},
{
"Time": "2012-11-27 16:24:55",
"NumPlayers": "1"
},
{
"Time": "2012-11-27 16:25:37",
"NumPlayers": "2"
},
{
"Time": "2012-11-27 16:29:33",
"NumPlayers": "2"
The times are MySQL timestamps. I need to get it into this format to use with Highcharts javascript charting.
data: [
[Date.UTC(1970, 9, 9), 0 ],
[Date.UTC(1970, 9, 14), 0.15],
[Date.UTC(1970, 10, 28), 0.35],
[Date.UTC(1970, 11, 12), 0.46],
I'm having trouble figuring out how to loop through the MySQL results and convert the timestamps to javascript Date.UTC objects. I need to place the NumPlayers value after the Date.UTC objects, and output it in the format below. I'm a PHP and javascript noob :\
It should look something like this:
data: [
[Date.UTC(2012, 11, 27, 16, 10, 35), 1],
[Date.UTC(2012, 11, 27, 16, 24, 55), 1],
[Date.UTC(2012, 11, 27, 16, 25, 37), 2],
[Date.UTC(2012, 11, 27, 16, 29, 33), 2],
You should realize that Date.UTC(2012, 11, 27, 16, 10, 35) simply returns the number of milliseconds since the epoch (1356624635000). Therefore, you can just convert your object into UNIX timestamps (times a 1000 since JS works with millisecond timestamps, but PHP works with seconds).
Sample Code
$data = '[{"Time": "2012-11-27 16:10:35", "NumPlayers": "1"}, {"Time": "2012-11-27 16:24:55", "NumPlayers": "1"}]';
// Make sure date is parsed as UTC
date_default_timezone_set("UTC");
// Convert items into the desired format
$mapper = function($item) {
return array(strtotime($item->Time)*1000, $item->NumPlayers);
}
echo json_encode(array_map($mapper, json_decode($data)));
Output
[[1354032635000,"1"],[1354033495000,"1"]]
You seem to be getting straight JSON from your database, which you can always convert into an array:
$data = '[{"Time": "2012-11-27 16:10:35", "NumPlayers": "1"}, {"Time": "2012-11-27 16:24:55", "NumPlayers": "1"}]';
$arrayData = json_decode($data, true);
After which you can simply iterate through the array and print out the contents of the array in the JS format you need. Something like that:
echo 'data: [' . PHP_EOL;
foreach ($arrayData as $item) {
echo '[Date.UTC(';
$tmp = preg_split( '/(-| |\:)/', $item['Time'] );
echo implode(', ', $tmp);
echo '), ' . $item['NumPlayers'] . '],';
echo PHP_EOL;
}
echo PHP_EOL . '];';
You can split the time string into sections using /[^\d]+/g:
var finalData = [], numbers=/[^\d]+/g, item, dateParts, newDate;
for (var i=0, l=data.length; i<l; i++) {
item = data[i];
dateParts = item.Time.split(numbers);
newDate = Date.UTC.apply(Date, dateParts);
finalData.push([newDate, item.NumPlayers]);
}
See also: MDN's documentation on JavaScript regular expressions (/[^\d]/g is a regex).
The best way to provide data to Highcharts is getting data formated from database, so that you don't have to do it on client side.
To do it you just have to change your query to something like the following.
Backend
$query = "SELECT UNIX_TIMESTAMP(date) * 1000 AS 'date', value FROM ...";
Then to send the result to frontend:
echo json_encode($result);
Frontend
To get the result on frontend, this case using jQuery and assuming that url is your url:
$.getJSON(url, function(json) {
// decode json
var data = $.parseJSON(json);
// Then you just have to pass it to your series
});
Or better, store date values in UTC.
var php_data = [
{
"Time": "2012-11-27 16:10:35",
"NumPlayers": "1"
},
{
"Time": "2012-11-27 16:24:55",
"NumPlayers": "1"
}
];
var length = php_data.length;
hc_data = [];
for(var i = 0; i< length; i++){
php_date = new Date(php_data[i]["Time"]);
hc_data.push(
[
Date.UTC(
php_date.getFullYear(),
php_date.getMonth() + 1,
php_date.getDate()
),
php_data[i]["NumPlayers"]
]
);
}
// hc_data is your Array
Related
I want to calcul the total availability of an user:
I store availibilty like this :
And I have history of availibilty cause user can change their availibilty so I need to make the sum with this.
signupDate to first UpdateDATE and after
updateDate[i] to updateDate[i+1]
and at the end
updateDate[i+n] to now;
and get for each duration in minute :
endHours - startHours
I got json like this :
availibility object are per week
{
"2022-12-20" (date when user have signup ): [
{
dayNumber : 2,
startHours:08:00,
endHours :10:00;
},
{
dayNumber : 3,
startHours:11:00,
endHours :16:00;
}
],
"2022-12-28" (date when user have update his availibilties): [
{
dayNumber : 2,
startHours:08:00,
endHours :10:00;
},
{
dayNumber : 3,
startHours:11:00,
endHours :16:00;
}
],
"2023-01-01" (date when user have update his availibilties): [
{
dayNumber : 5,
startHours:05:00,
endHours :10:00;
},
{
dayNumber : 7,
startHours:19:00,
endHours :22:00;
}
]
}
Whats I have start for the moment :
I have count number of weeks beetwen signupDate and now :
$number_of_week = Carbon::parse($user->signupDate)->diffInWeeks(Carbon::now());
Finaly I want to get total availibilty of the user
Thanks
After our chat, I think I got the full picture of the problem, so lets see if we can solve this together:
I'm not sure where the json comes from, so let's just assign it to a variable for now:
$jsonString = '
{
"2022 - 12 - 20": [
{
"dayNumber": 2,
"startHours": "08:00",
"endHours": "10:00"
},
{
"dayNumber": 3,
"startHours": "11:00",
"endHours": "16:00"
}
],
"2022 - 12 - 28": [
{
"dayNumber": 2,
"startHours": "08:00",
"endHours": "10:00"
},
{
"dayNumber": 3,
"startHours": "11:00",
"endHours": "16:00"
}
],
"2023 - 01 - 01": [
{
"dayNumber": 5,
"startHours": "05:00",
"endHours": "10:00"
},
{
"dayNumber": 7,
"startHours": "19:00",
"endHours": "22:00"
}
]
}
';
Then, we can run the code like so:
//I'm assuming that the json only contains availabilities for 1 user
//This creates an associated array out of your json
$assocArray = json_decode($jsonString, true);
$totalAvailabilityInMinutes = 0;
foreach($assocArray as $updatedAvailabilityAt => $availabilityArray) {
$availabilityOfDayInMinutes = 0;
foreach($availabilityArray as $availability) {
$explodedStart = explode(':', $availability['startHours']);
$explodedEnd = explode(':', $availability['endHours']);
//Perform calculation from my comment
$availabilityOfDayInMinutes = ($explodedEnd[0] * 60 + $explodedEnd[1]) - ($explodedStart[0] * 60 + $explodedStart[1]);
dump("Availability of day {$availability['dayNumber']}: $availabilityOfDayInMinutes");
$totalAvailabilityInMinutes += $availabilityOfDayInMinutes;
}
}
dump($totalAvailabilityInMinutes);
Please note that I used 2 variables to store the minutes, one per day, and one accumulating the days. You can pick which one is most applicable to you.
I've tested the code locally, it should work and provide correct numbers ;)
Edit:
So since you already have an array, not a json, you could skip this step:
$assocArray = json_decode($jsonString, true);
I have a JSON file that I created, and I want to sort from the biggest price value.
JSON is like this:
[
{
"username": "elbugato",
"sold": 19,
"qmimi": 38.5
},
{
"username": "Pablo",
"sold": 12,
"qmimi": 42
},
{
"username": "Hqstuff",
"sold": 0,
"qmimi": "0"
},
{
"username": "matchspamm3rs",
"sold": 0,
"qmimi": "0"
},
{
"username": "Pachenko",
"sold": 1,
"qmimi": 1.1
},
I want to sort qmimi from the highest value
My php code is this.
$sellertop8json = json_decode(get_html('link'));
$i = 1;
sort($sellertop8json->qmimi, SORT_NUMERIC);
foreach($sellertop8json as $top8){
max($top8);
if (++$i == 8) break;
echo '<tr>
<td>'.$top8->username.'</td>
<td># '.$top8->sold.'</td>
<td>$ '.$top8->qmimi.'</td>
</tr>
';
}
but they aren't sorting from the biggest value
The results I get :
Look at "Pachenko", he is after a seller that has "0" Earned.
Thank You
Sorry for my bad English
P.S : JSON ISN'T RAW, I copied from some extension I am using on google chrome, so JSON it's not the problem.
You need to use usort and provide custom function comparing the field of the element:
usort($sellertop8json, function($a, $b) {
return $a->qmimi == $b->qmimi ? 0 :
$a->qmimi < $b->qmimi ? 1 : -1;
}
);
The comparison function must return 0 if the elements are equal, less than 0 if the first element is lower and greater than 0 if the second element is higher.
This may be a bit confusing as you're sorting from highest and therefore the swap of the sign in comparison - see the last part of comparison.
I'm trying to get an array into JSON so I can read it out correctly into a Python script. I've managed to get the URL to output data but I'm not sure that it's putting out the correct format. I'm getting an 'Invalid encoding, expecting UTF-8, UTF-16 or UTF-32.' error when I run it through a JSON Parser.
Here's the result that I'm getting from the URL....
[
{
"year":"2016",
"Month":"Apr",
"the_days":"16, 29, 30"
},
{
"year":"2016",
"Month":"May",
"the_days":"13, 27"
},
{
"year":"2016",
"Month":"Jun",
"the_days":"10, 11, 24"
},
{
"year":"2016",
"Month":"Jul",
"the_days":"08, 22, 23"
},
{
"year":"2016",
"Month":"Aug",
"the_days":"06, 20"
},
{
"year":"2016",
"Month":"Sep",
"the_days":"02, 03, 16, 17, 30"
},
{
"year":"2016",
"Month":"Oct",
"the_days":"01, 14, 15, 29"
},
{
"year":"2016",
"Month":"Nov",
"the_days":"25"
},
{
"year":"2016",
"Month":"Dec",
"the_days":"09, 10, 23, 24"
}
]
This is achieved by putting the data into an array like this....
$encodeArray = array();
while($row = $result->fetch_assoc()) {
$encodeArray[] = $row;
}
header("Content-Type: application/json; charset=utf-8");
I'm having trouble figuring out what's wrong with this - could do with some help!
Use json_encode function to convert your array to json string in php and print it on page using echo statement. So your script can get response
<?php
$json_arr = array();
while($row = $result->fetch_assoc()) {
array_push($json_arr,$row);
}
echo json_encode($json_arr);
?>
It's looking more and more like there is nothing wrong with my array - it's possible that the problem is happening in my Python script that won't decode the json.
Separate question to follow!
I have to show some results of an experiment in a line chart. I got it working for static data, but I want to make it dynamic.
So what I got:
I have a line charts:
var options = {
chart: {
renderTo: 'container',
type: 'line',
},
series: [{}]
}
I want something like this: http://jsfiddle.net/gh/get/jquery/1.7.1/highslide-software/highcharts.com/tree/master/samples/highcharts/series/data-array-of-arrays/
I am struggling to make those data:
data: [
[0, 29.9],
[1, 71.5],
[3, 106.4]
]
in php side and send it jquery, and feed to my line chart.
I am aware of JSON encoding, but how should I create and array in php in first place?
And how should I encode it and give it to jquery and feed my line chart?
This is working so far with static data:
var arr = [[0, 15], [10, 50], [20, 56.5], [30, 46.5], [40, 22.1],
[50, 2.5], [60, 27.7], [70, 55.7], [80, 76.5]];
options.series[0].name = 'Occlusion';
options.series[0].data = arr;
var l = new Highcharts.Chart(options);
});
But I want arr to come from php.
Thanks in advance.
If you want to create such format, you need to restructure them first. Consider this example:
<?php
// lets say you have a raw data like this
$php_values = array(
array(0, 15),
array(10, 50),
array(20, 56.5),
array(30, 46.5),
array(40, 22.1),
array(50, 2.5),
array(60, 27.7),
array(70, 55.7),
array(80, 76.5),
);
$temp = array();
// format each array
foreach($php_values as $key => $value) {
$temp[] = '['.implode(',', $value).']';
}
// then format the whole array
$formatted_values = '['.implode(',', $temp).']';
?>
<script type="text/javascript">
var values = <?php echo $formatted_values; ?>;
console.log(values);
</script>
Sample Output
Edit: as #SebastianBochan pointed out, $formatted_values = json_encode($php_array) works the same way (probably the best and cleanest way)
I have invalid external json data, without double quotes around names.
Example:
{
data: [
{
idx: 0,
id: "0",
url: "http://247wallst.com/",
a: [
{
t: "Title",
u: "http://247wallst.com/2012/07/30/",
sp: "About"
}
],
doc_id: "9386093612452939480"
},
{
idx: 1,
id: "-1"
}
],
results_per_page: 10,
total_number_of_news: 76,
news_per_month: [20, 0, 8, 1, 1, 2, 0, 2, 1, 0, 0, 1, 1, 0, 5, 1, 1, 1, 0, 2, 5, 16, 7, 1],
result_start_num: 2,
result_end_num: 2,
result_total_articles: 76
}
As you see a lot of names like data,idx,id,url and others are not double quoted, so this makes this json invalid.
How can I make this external json valid? I already tried str_replace, replacing '{' to '{"' and ':' to '":' adding double quotes around unquoted names, but this messes up some already double quoted variables.
How can I make this json valid so I can read this data with PHP json_decode? I'm not very familiar with preg_replace..
Valid json will look like:
{
"data": [
{
"idx": 0,
"id": "0",
"url": "http://247wallst.com/",
"a": [
{
"t": "Title",
"u": "http://247wallst.com/2012/07/30/",
"sp": "About"
}
],
"doc_id": "9386093612452939480"
},
{
"idx": 1,
"id": "-1"
}
],
"results_per_page": 10,
"total_number_of_news": 76,
"news_per_month": [20, 0, 8, 1, 1, 2, 0, 2, 1, 0, 0, 1, 1, 0, 5, 1, 1, 1, 0, 2, 5, 16, 7, 1],
"result_start_num": 2,
"result_end_num": 2,
"result_total_articles": 76
}
Please suggest me some php preg_replace function.
Data source:
http://www.google.com/finance/company_news?q=aapl&output=json&start=1&num=1
With preg_replace you can do:
json_decode(preg_replace('#(?<pre>\{|\[|,)\s*(?<key>(?:\w|_)+)\s*:#im', '$1"$2":', $in));
Since the above example won't work with real data (the battle plans seldom survive first contact with the enemy) heres my second take:
$infile = 'http://www.google.com/finance/company_news?q=aapl&output=json&start=1&num=1';
// first, get rid of the \x26 and other encoded bytes.
$in = preg_replace_callback('/\\\x([0-9A-F]{2})/i',
function($match){
return chr(intval($match[1], 16));
}, file_get_contents($infile));
$out = $in;
// find key candidates
preg_match_all('#(?<=\{|\[|,)\s*(?<key>(?:\w|_)+?)\s*:#im', $in, $m, PREG_OFFSET_CAPTURE);
$replaces_so_far = 0;
// check each candidate if its in a quoted string or not
foreach ($m['key'] as $match) {
$position = $match[1] + ($replaces_so_far * 2); // every time you expand one key, offsets need to be shifted with 2 (for the two " chars)
$key = $match[0];
$quotes_before = preg_match_all('/(?<!\\\)"/', substr($out, 0, $position), $m2);
if ($quotes_before % 2) { // not even number of not-escaped quotes, we are in quotes, ignore candidate
continue;
}
$out = substr_replace($out, '"'.$key.'"', $position, strlen($key));
++$replaces_so_far;
}
var_export(json_decode($out, true));
But since google offers this data in RSS feed, i would recommend you to use that one if it works for your usecase, this is just for fun (-:
The JSON feeds from Google always seem to be plagued with problems- formatted incorrectly in some way shape or form. If you switch the feed to RSS you can easily convert it to an array or JSON from the array.
<?php
$contents = file_get_contents('http://www.google.com/finance/company_news?q=aapl&output=rss&start=1&num=1');
// Convert the RSS to an array (probably just use this)
$arr = simplexml_load_string($contents);
// Or if you specifically want JSON
$json = json_encode($arr);
// And back to an array
print_r(json_decode($json));