Best way to define months as array elements from a date range - php

I trying to define an array containing all months from a certain date range (like 2015-03-01 and 2017-03-01).
The result I'm looking for is:
Array
(
['2015'] => Array
(
['03'] => 0
['04'] => 0
['05'] => 0
['06'] => 0
['07'] => 0
['08'] => 0
['09'] => 0
['10'] => 0
['11'] => 0
['12'] => 0
)
['2016'] => Array
(
['01'] => 0
['02'] => 0
['03'] => 0
['04'] => 0
['05'] => 0
['06'] => 0
['07'] => 0
['08'] => 0
['09'] => 0
['10'] => 0
['11'] => 0
['12'] => 0
)
['2017'] => Array
(
['01'] => 0
['02'] => 0
['03'] => 0
)
)
Which way would be the best way to do it?
Note: This is a dummy example, just looking for the best practice.

Some simple things to start off with
use 32-bit int instead of strings
make the entire things with the years, months and date on Continuos memory (very good for caching)
if possible make it matrix-free, that means you know a year has 12 months; so why bother reserving memory for that redundant information?

Related

Sorting 3 or more values with tiebreakers in PHP

I'm ranking a list based on a set of tie-breaking criteria using usort in PHP. My problem is that the tiebreakers aren't applied correctly when there are 3 or more ties to break. The function only sorts between 2 entries as it iterates through the array.
When 2 entries have identical Overall wins, the function checks Division wins; if those are identical, it checks Head to head; then Points if the tie still is not broken. This works great to break the tie between 2 entries, but with 3 or more ties, the iterative nature of this process is an issue.
Example:
3 entries have identical Overall wins and Division wins, so we check Head to head results.
Team 1 beat Team 2.
Team 2 beat Team 3.
Team 3 beat Team 1.
Therefore, we should move on to Points because the tie cannot be broken with Head to head as each team has one win over the other two teams. But my function doesn't ever make that third check, I guess because Team A and Team C aren't next to each other when usort() does it's thing...I'm not sure.
Here is the comparison function I pass into usort:
function set_division_order($a, $b) {
if ($a['wins'] == $b['wins'] && $a['losses'] == $b['losses']) { //same overall record
if ($a['div_wins'] == $b['div_wins'] && $a['div_losses'] == $b['div_losses']) { //same division record
$h2h = get_h2h_result($year, $a['team_id'], $b['team_id']);
if ($h2h == 0) { //same head-to-head record
return ($b['pts'] - $a['pts']);
}
else { //head-to-head winner
return $h2h;
}
}
else { //different division record
return ($b['div_wins'] - $a['div_wins']);
}
}
else { //different overall record
return ($b['wins'] - $a['wins']);
}
}
The array getting passed into usort($array, 'set_division_order') which is causing problems:
Array
(
[0] => Array
(
[team_id] => 1
[wins] => 3
[losses] => 2
[ties] => 0
[div_wins] => 3
[div_losses] => 2
[div_ties] => 0
[pts] => 513.9
)
[1] => Array
(
[team_id] => 2
[wins] => 3
[losses] => 2
[ties] => 0
[div_wins] => 3
[div_losses] => 2
[div_ties] => 0
[pts] => 504.1
)
[2] => Array
(
[team_id] => 3
[wins] => 3
[losses] => 2
[ties] => 0
[div_wins] => 3
[div_losses] => 2
[div_ties] => 0
[pts] => 517.7
)
[3] => Array
(
[team_id] => 4
[wins] => 4
[losses] => 1
[ties] => 0
[div_wins] => 4
[div_losses] => 1
[div_ties] => 0
[pts] => 491.9
)
[4] => Array
(
[team_id] => 5
[wins] => 2
[losses] => 3
[ties] => 0
[div_wins] => 2
[div_losses] => 3
[div_ties] => 0
[pts] => 393.3
)
[5] => Array
(
[team_id] => 6
[wins] => 0
[losses] => 5
[ties] => 0
[div_wins] => 0
[div_losses] => 5
[div_ties] => 0
[pts] => 377.9
)
)
Sorry this was kind of long, but hopefully the problem is pretty evident. Now it's just time for a solution! I've scoured the PHP resources for the various sort functions, with no luck. I can't be the only one trying to break a 3-way tie with PHP. :)
If I can get just the tied teams matching that criteria into a separate array, I could use a function to re-sort the original sorted array against the values of this new sub-array to order the tied teams within the main array.
...And my head just exploded.
Thanks!

how to get a value of an array while unserialize the array

I have an serialized array like below:
a:22:{s:18:"'myprofiledefault'";s:1:"2";s:19:"'myprofilepersonal'";s:1:"0";s:14:"'myprofilejob'";s:1:"0";s:16:"'myprofileleave'";s:1:"0";s:21:"'myprofilepermission'";s:1:"0";s:28:"'myprofilebonus & commision'";s:1:"0";s:19:"'myprofiledocument'";s:1:"0";s:28:"'myprofileemergency contact'";s:1:"0";s:19:"'myprofilebenifits'";s:1:"0";s:17:"'view empdefault'";s:1:"0";s:18:"'view emppersonal'";s:1:"0";s:13:"'view empjob'";s:1:"0";s:15:"'view empleave'";s:1:"0";s:20:"'view emppermission'";s:1:"0";s:27:"'view empbonus & commision'";s:1:"0";s:18:"'view empdocument'";s:1:"0";s:27:"'view empemergency contact'";s:1:"0";s:18:"'view empbenifits'";s:1:"0";s:15:"'view empnotes'";s:1:"0";s:17:"'view emponboard'";s:1:"0";s:18:"'view empoffboard'";s:1:"0";s:16:"'view empcharts'";s:1:"0";}
If i unserialized this and print means it will be look like below:
Array(['myprofiledefault'] => 2
['myprofilepersonal'] => 0
['myprofilejob'] => 0
['myprofileleave'] => 0
['myprofilepermission'] => 0
['myprofilebonus & commision'] => 0
['myprofiledocument'] => 0
['myprofileemergency contact'] => 0
['myprofilebenifits'] => 0
['view empdefault'] => 0
['view emppersonal'] => 0
['view empjob'] => 0
['view empleave'] => 0
['view emppermission'] => 0
['view empbonus & commision'] => 0
['view empdocument'] => 0
['view empemergency contact'] => 0
['view empbenifits'] => 0
['view empnotes'] => 0
['view emponboard'] => 0
['view empoffboard'] => 0
['view empcharts'] => 0)
My question is that i want to get the key values individually.
I am trying this one
echo $ret['myprofilepersonal'];
but this is not working showing an error undefined index. Please How to get this
If we suppose that you unserialize the array you posted like so:
$ret = unserialize(...);
Then you need to access the values using sth like this:
echo $ret["'myprofiledefault'"];
// or by escaping single quote:
echo $ret['\'myprofiledefault\''];
because as I see, every key is quoted

Why does my php page seem to load async?

I have two cpanel websites running the same php script that parses a csv file, one on godaddy servers (site A), one on hostgator (site B). on site B, the html portion of the page renders and then it parses the csv, while on site A it parses the csv and then renders the html portion.
Any ideas why there is such different behavior? I would like to have Site A perform this script like Site B
They are both running php 5.4 and have the same execution time limits.
Below are the result of array_diff_assoc of php.ini arrays of name => local_value. The first is array_diff_assoc(Site B, Site A) the second is array_diff_assoc(Site A, Site B)
Array
(
[allow_url_include] => 1
[date.timezone] => America/Chicago
[disable_functions] => dl
[enable_dl] =>
[error_reporting] => 22519
[expose_php] =>
[extension_dir] => /opt/php54/lib/php/extensions/no-debug-non-zts-20100525
[include_path] => .:/opt/php54/lib/php
[intl.default_locale] =>
[intl.error_level] => 0
[max_execution_time] => 30
[memory_limit] => 256M
[mssql.allow_persistent] => 1
[mssql.batchsize] => 0
[mssql.charset] =>
[mssql.compatability_mode] =>
[mssql.connect_timeout] => 5
[mssql.datetimeconvert] => 1
[mssql.max_links] => -1
[mssql.max_persistent] => -1
[mssql.max_procs] => -1
[mssql.min_error_severity] => 10
[mssql.min_message_severity] => 10
[mssql.secure_connection] =>
[mssql.textlimit] => -1
[mssql.textsize] => -1
[mssql.timeout] => 60
[mysql.allow_persistent] =>
[odbc.allow_persistent] =>
[odbc.check_persistent] =>
[odbc.default_cursortype] => 3
[odbc.default_db] =>
[odbc.default_pw] =>
[odbc.default_user] =>
[odbc.defaultbinmode] => 1
[odbc.defaultlrl] => 4096
[odbc.max_links] => -1
[odbc.max_persistent] => -1
[pcre.backtrack_limit] => 1000000
[pcre.recursion_limit] => 100000
[post_max_size] => 64M
[sourceguardian.restrict_unencoded] => 0
[upload_max_filesize] => 64M
[xsl.security_prefs] => 44
)
Array
(
[allow_url_include] => 0
[apc.cache_by_default] => 1
[apc.canonicalize] => 1
[apc.coredump_unmap] => 0
[apc.enable_cli] => 0
[apc.enabled] => 1
[apc.file_md5] => 0
[apc.file_update_protection] => 2
[apc.filters] =>
[apc.gc_ttl] => 3600
[apc.include_once_override] => 0
[apc.lazy_classes] => 0
[apc.lazy_functions] => 0
[apc.max_file_size] => 1M
[apc.mmap_file_mask] =>
[apc.num_files_hint] => 1000
[apc.preload_path] =>
[apc.report_autofilter] => 0
[apc.rfc1867] => 0
[apc.rfc1867_freq] => 0
[apc.rfc1867_name] => APC_UPLOAD_PROGRESS
[apc.rfc1867_prefix] => upload_
[apc.rfc1867_ttl] => 3600
[apc.serializer] => default
[apc.shm_segments] => 1
[apc.shm_size] => 32M
[apc.shm_strings_buffer] => 4M
[apc.slam_defense] => 1
[apc.stat] => 1
[apc.stat_ctime] => 0
[apc.ttl] => 0
[apc.use_request_time] => 1
[apc.user_entries_hint] => 4096
[apc.user_ttl] => 0
[apc.write_lock] => 1
[date.timezone] => UTC
[disable_functions] =>
[enable_dl] => 1
[error_reporting] => 1
[expose_php] => 1
[extension_dir] => /usr/local/lib/php/extensions/no-debug-non-zts-20100525
[include_path] => .:/usr/lib/php:/usr/local/lib/php
[max_execution_time] => 120
[memory_limit] => 64M
[mysql.allow_persistent] => 1
[mysqlnd.collect_memory_statistics] => 0
[mysqlnd.collect_statistics] => 1
[mysqlnd.debug] =>
[mysqlnd.log_mask] => 0
[mysqlnd.mempool_default_size] => 16000
[mysqlnd.net_cmd_buffer_size] => 4096
[mysqlnd.net_read_buffer_size] => 32768
[mysqlnd.net_read_timeout] => 31536000
[pcre.backtrack_limit] => 10000000
[pcre.recursion_limit] => 10000000
[post_max_size] => 48M
[upload_max_filesize] => 32M
)
Update 1
In comparing ini_get_all, I have found that Site A has APC enabled where Site B does not... could this be the issue? Is there any harm in this implementation in disabling APC?
Update 2
I believe we can rule out implicit_flush, as both are set to false and setting Site A's to true does not change the behavior
Update 3
Included the differences in ini files
Update 4
I have set Site A's php.ini file to be the same as site B's, with no change in behavior, so maybe we can rule this out?
Update 5
Although it does not mimic the async nature of site B, using
ob_flush();
flush();
to flush the buffer seems to get it very close, but it is not a very clean solution...

Replace array element with last non zero value

I have a result array like the following :
Array
(
[2013-02-27] => Array
(
[CPD] => 0
[Display] => 0
[Incent] => 0
[Organic] => 1464
[uncategorized] => 0
[total] => 0
)
[2013-02-26] => Array
(
[CPD] => 0
[Display] => 0
[Incent] => 0
[Organic] => 1463
[uncategorized] => 0
[total] => 0
)
[2013-02-25] => Array
(
[CPD] => 0
[Display] => 0
[Incent] => 0
[Organic] => 1459
[uncategorized] => 0
[total] => 0
)
[2013-02-24] => Array
(
[CPD] => 0
[Display] => 2
[Incent] => 0
[Organic] => 1449
[uncategorized] => 0
[total] => 0
)
This is basically a running total thing and I need to have the zero values replaced by the higher value if there is any. I mean I have 2 as the count for 2013-02-24 for Display. So I need to have this has to be the count for other dates for display category. For making things a little more clear, this is the number of downloads for a product and I am calculating the count upto each day from various sources. So if it has a count of 2 upto 24th, it has to be the same upto the coming days if there the downloads for the day is zero. If downloads happen again it will added up. So what I am doing is I am looping the array and checking for zero values. If the value is zero for a particular section say Display, it has to be replaced with the higher number from the previous date if there is any. So how can I do this ?
I have used the following code :
foreach ($result as $key=>$value){
foreach($categories as $cat){
if($value[$cat->category]==0){
$prev_day = date("Y-m-d",strtotime ( '-1 day' , strtotime ( $key ) )) ;
while ($prev_day>=$from) {
if($result[$prev_day][$cat->category]!=0){
$prev_high_value[$cat->category]=$result[$prev_day][$cat->category];
break;
}
$prev_day = strtotime ( '-1 day' , strtotime ( $prev_day ) ) ;
}
$result[$key][$cat->category]=$prev_high_value[$cat->category];
}
}
}
where $category is an object with the category values like Display an others

Sort multidimensional array (PHP) - date complications and counting

I have the following output of an array using PHP. I need to do two things... First, I need to sort the array so it prints by the most recent date. I can't use a simple sort, because the date is outputted in the format mm/dd/yyyy (and not a regular time stamp) ...
Then I need to count how many rows exist for each year.
So, in the example below, I would need to know that there are ...
2 entries from 2010
2 entries from 2011
1 entry from 2012
Stop counting when there are no more rows
Since the year is not separate from the rest of the date digits, this also complicates things...
Array
(
[0] => Array
(
[racer_date] => 11/15/2010
[racer_race] => Test Row 4
[racer_event] => 321
[racer_time] => 16
[racer_place] => 12
[racer_medal] => 1
)
[1] => Array
(
[racer_date] => 7/15/2010
[racer_race] => Test Row 3
[racer_event] => 123
[racer_time] => 14
[racer_place] => 6
[racer_medal] => 0
)
[2] => Array
(
[racer_date] => 7/28/2011
[racer_race] => Test Row
[racer_event] => 123
[racer_time] => 10
[racer_place] => 2
[racer_medal] => 2
)
[3] => Array
(
[racer_date] => 10/9/2011
[racer_race] => Test Row 2
[racer_event] => 321
[racer_time] => 12
[racer_place] => 3
[racer_medal] => 3
)
[4] => Array
(
[racer_date] => 10/3/2012
[racer_race] => World Indoor Championships (final)
[racer_event] => 400m
[racer_time] => 50.79
[racer_place] => 1
[racer_medal] => 1
)
)
function cmp($a, $b)
{
if (strtotime($a["racer_date"]) == strtotime($b["racer_date"])) {
return 0;
}
return (strtotime($a["racer_date"]) < strtotime($b["racer_date"])) ? -1 : 1;
}
usort($array, "cmp");
call your array $array, and above code will sort it..
And to count entities you'll need to run foreach and check date('Y',strtotime($a["racer_date"])) in that foreach which will give you year in 4 digit..

Categories