This is somewhat basic but one of my first PHP projects and I'm stuck. I need to create something that will iterate through a series of DateTime objects and sort them into groups based on the interval of time between each object, with the end goal being to average data associated with these groups of dates (just focusing on how to group the data in this post). If the DateTime objects are more than 1 day apart, then they need to be in a separate sub-group but still a part of the larger list of dates.
I have been able to create DateInterval objects from the DateTime objects. Below is an example of two DateTime objects pulled from a larger list of dates and the corresponding DateInterval generated from the amount of time between the two`
[4] => DateTime Object
(
[date] => 2017-04-21 12:50:53.000000
[timezone_type] => 3
[timezone] => America/Denver
)
[5] => DateTime Object
(
[date] => 2015-04-18 12:50:53.000000
[timezone_type] => 3
[timezone] => America/Denver
)
`[9] => DateInterval Object
(
[y] => 2
[m] => 0
[d] => 3
[h] => 0
[i] => 0
[s] => 0
[weekday] => 0
[weekday_behavior] => 0
[first_last_day_of] => 0
[invert] => 1
[days] => 734
[special_type] => 0
[special_amount] => 0
[have_weekday_relative] => 0
[have_special_relative] => 0
)`
Is there a way I could create some sort of array that holds subgroups of date objects in their own array that are sorted based on the size of their intervals? I illustrated the structure of what I'm thinking below.
Array [
Array [
[date1],
[date2],
[date3]
]
Array [
[date4],
[date5],
]
Array [
[date6]
]
]
First, sort your array of dates (if they aren't already sorted).
sort($dates);
Then keep track of the previous item as you iterate the array. If the number of days in the diff interval exceeds your threshold then increment the key used to define the groups in the result array.
$i = 0;
$previous = null;
foreach ($dates as $date) {
if ($previous && $date->diff($previous)->days > 1) {
$i++;
}
$groups[$i][] = $date;
$previous = $date;
}
This is the code that solved my problem, which was made from a slight modification of #Don'tPanic's code.
$incrementKey = 0;
foreach ($datesIntervalArray as $d) {
if ($d->days > 1) {
$incrementKey++;
}
$groupsOfVisits[$incrementKey][] = $d;
`
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!
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?
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..
I have this array that I am displaying with a table how can i use user input for movement
currently 0 is assigned to every array but I plan on assigning other values to the array:
my question is - how can i move up, down, right, left, and move diagonally within the array using user input
Array ( [0] => Array ( [0] => 0 [1] => 0 [2] => 0 [3] => 0 [4] => 0 [5] => 0 [6] => 0 [7] => 0 )
[1] => Array ( [0] => 0 [1] => 0 [2] => 0 [3] => 0 [4] => 0 [5] => 0 [6] => 0 [7] => 0 )
[2] => Array ( [0] => 0 [1] => 0 [2] => 0 [3] => 0 [4] => 0 [5] => 0 [6] => 0 [7] => 0 )
[3] => Array ( [0] => 0 [1] => 0 [2] => 0 [3] => 0 [4] => 0 [5] => 0 [6] => 0 [7] => 0 )
[4] => Array ( [0] => 0 [1] => 0 [2] => 0 [3] => 0 [4] => 0 [5] => 0 [6] => 0 [7] => 0 )
[5] => Array ( [0] => 0 [1] => 0 [2] => 0 [3] => 0 [4] => 0 [5] => 0 [6] => 0 [7] => 0 )
[6] => Array ( [0] => 0 [1] => 0 [2] => 0 [3] => 0 [4] => 0 [5] => 0 [6] => 0 [7] => 0 )
[7] => Array ( [0] => 0 [1] => 0 [2] => 0 [3] => 0 [4] => 0 [5] => 0 [6] => 0 [7] => 0 )
);
array(0,0,0,0,0,0,0,0),
array(0,0,0,0,0,0,0,0),
array(0,0,0,0,0,0,0,0),
array(0,0,0,0,0,0,0,0),
array(0,0,0,0,0,0,0,0),
array(0,0,0,0,0,0,0,0),
array(0,0,0,0,0,0,0,0),
array(0,0,0,0,0,0,0,0),
It is for a checkers game no mysql.
I can already serialize the array into text file, but the text files needs to contain the start position and when each player makes a move put the location the piece move to in the text file then call back to the display
and I have already displayed the array into an html table
I am also trying to restrict movement to illegal square but that's a logic problem i need work on myself
will this loop work with code below
$row = 0;
print "<form>";
print "<table border = 1>";
while ($row < 8){ // Counts to 8. (from 0...7 = 8 times. 0 ... 8 = 9 times)
print "<tr>";
$row++;
$col = 0; // reset column to 0 each time printing one row.
while ($col < 8){
print "<td>";
if($board[$row][$col] == 0)
{
print "<input type=\"checkbox\" name=\"box[]\" value=\"$value\">";
// Add \ before " otherwise it will treat as the end of the quote.
}
print "</td>";
$col++;
}
print "</tr>";
}
print "</table>";
print "</form>";
I already created a database for keeping score but that will be finished after this
You need to define the available movement for the game, in this case, from the players point of view you can say that a player can move it's piece:
up-left
up-right
up-left-up-left
up-right-up-right
Note that the two last elements of the list are those of one piece eating another one. Once you know that you can take the current position of the piece and move it to the new one. I'm going to assume that for normal pieces you would use "N" and for queens "Q" although I will not use queens in my examples.
I will use a normal move and then an actual eating one:
//Piece at $board[$x][$y] moves diagonally to the left.
$board[$x-1][$y+1] = $board[$x][$y]; // This space is occupied
$board[$x][$y] = 0; //Now the space is empty
Now for the eating part. Lets imagine that the piece on $board[$x][$y] wants to eat the one that's in diagonally left.
//Eating action from $board[$x][$y]
$board[$x-1][$y+1] = 0; //It's been eaten!
$board[$x-2][$y+2] = $board[$x][$y]; // This space is occupied
So you could get an input from the user that included, the piece he wants to move, and what kind of movements he wants to do (I'm assuming, you will only allow the correct moves so I will not get into that). If you are reading it from a form submit for example you could get the movement, the position and the player (for orientation) as $_POST variables.
Then depending on those values modify the $board array. To do so, you could use conditionals or a switch, that's up to you.
$way = ($_POST['player'] === 'up')? 1:-1;
That last line will allow you to re-use the same code for the movements, multiplying the values you have to add to the current position to get to the new one, by the $way variable. For instance, going diagonally left would be:
//if player is 'up' then the value of $way is 1 so
$board[$x+(-1*$way)][$y+(1*$way)] = $board[$x][$y]; // position 2,2 becomes 1,3
//if player is not 'up' then the value of $way is -1 so
$board[$x+(-1*$way)][$y+(1*$way)] = $board[$x][$y]; // position 2,2 becomes 3,1
This should give you a starting point, all code was un-tested so I guess there may be some typos.
UPDATE
If all you want is to move from X,Y to X1Y1 then:
$board[$var3][$var4] = $board[$var1][$var2];
$board[$var1][$var2] = 0;
Is about all you need. :)
You can access every field of the board using its coordinates:
$array[$y][$x]
So, if you want to move something up, you can simply do:
$array[$y-1][$x] = $array[$y][$x];
$array[$y][$x] = 0;
I guess first you should fill with 1 and 2 the cells corresponding to each type of tile. Then you can assign numbers 3 and 4 to the ones that are coronated.
1.You can ask each player for example staring x,y and ending x,y
Then for doing a movment you first need to check it is allowed. Obviously starting $array[$x][$y] for player 1 should contain a 1. Then You need to make rules for this, For example if you are player 1 you can only go from $array[$x][$y] to $array[$x-1][$x-1], $array[$x-1][$y*1], etc when the place you wanna go is empty (filled with 0) Then you can check if more complicated moves are allowed like eating other player tile (which requires for example if you are player 1 to check things like $array[$x-2][$y-2] equals 0 and $array[$x-1][$y-1] equals 2. Then there is a series of more complicated verifications for the coronated ones that you should write. (besides always remember that you are moving within the limits of the array dimensions).
Finally you should alter the array cells that should be modified with the corresponding new values.