January 6th, 2018, 04:21 PM
-
Month-ify an array
Well, for only a single page, atm, tables are to be built in a multiple, per-month, action. The returned result from the database is the result set for a requested year.
I think this just needs 2 parts. 1.) A check/trigger of some kind that it is this sort of array vs any other to initiate TRUE in perhaps an if/else. 2.) A loop to rebuild itself, making the outer-most index numerically defined via month from the 2nd/'date' index.
The else may simply be omitted since in any other case, a foreach() to build the tables will use the same data, and just loop once.
I feel this will be a quick simple thing, but am probably just putting too much thought into the idea... ^_^
He who knows not that he knows not is a fool, ignore him. He who knows that he knows not is ignorant, teach him. He who knows not that he knows is asleep, awaken him. He who knows that he knows is a leader, follow him.
January 6th, 2018, 04:42 PM
-
January 6th, 2018, 05:00 PM
-
It will start as a basic result set like the upper example, and I need to group it by month, such as the lower example, with the numerical index matching the included month.
PHP Code:
array(
[0] => array(title, 01/01/2018, something, else),
[1] => array(title, 01/03/2018, something, else),
[2] => array(title, 05/01/2018, something, else),
[3] => array(title, 06/08/2018, something, else),
[4] => array(title, 06/23/2018, something, else)
)
array(
[1] => array(
[0] => array(title, 01/01/2018, something, else),
[1] => array(title, 01/03/2018, something, else)
),
[5] => array(
[0] => array(title, 05/01/2018, something, else)
),
[6] => array(
[0] => array(title, 06/08/2018, something, else),
[1] => array(title, 06/23/2018, something, else)
)
)
Last edited by Triple_Nothing; January 6th, 2018 at 05:06 PM.
He who knows not that he knows not is a fool, ignore him. He who knows that he knows not is ignorant, teach him. He who knows not that he knows is asleep, awaken him. He who knows that he knows is a leader, follow him.
January 6th, 2018, 07:10 PM
-
Then foreach + lookup array based on the month number.
Code:
result = []
foreach (array as key => value) {
m = the month number
if (!isset(result[m])) result[m] = []
result[m][] = value
}
Same basic structure as for all "group by"-type operations: start with an empty array, loop, derive the appropriate key from each value, check that the final array has that key or else add it with an empty array, then append to that array.
January 6th, 2018, 07:51 PM
-
Yep. Figured I was over-thinking it. Thank you very much!
He who knows not that he knows not is a fool, ignore him. He who knows that he knows not is ignorant, teach him. He who knows not that he knows is asleep, awaken him. He who knows that he knows is a leader, follow him.