php - Adding multiple arrays to same key
This seems like a relatively easy thing to do but I'm struggling a bit. Here is a bit of backstory. I'm currently making a schedule of events based off a web service.
Because of the way it outputs data I've been creating and rearranging the information based off the clients wants/needs.
I've created a time array that basically loops through the open hours in 15 min intervals. Example...
Array
(
[0600] => 0
[0615] => 0
[0630] => 0
[0645] => 0
[0700] => 0
[0715] => 0
[0730] => 0
[0745] => 0
[0800] => 0
[0815] => 0
[0830] => 0
[0845] => 0
[0900] => 0
...etc etc
[2300] => 0
)
Once I've done that I loop(using a foreach) through events and if the time is equal to a key add it to the array. The variables are just items from web service.
$timeArray[date('Hi', $roundStart)] = array(
'e_name' => $eventName,
'e_build_id' => $buildId,
'e_start' => $timeStampS,
'e_end' => $timeStampE,
'e_class' => $eClass,
'e_span' => $fullSpan,
'e_status' => $canCheck,
'e_lanes' => $lanesOpen
);
But I've noticed there is a bug with this and that if you have two events at the same time the last one will override the other one. What I need instead is for the events to be within the same key. So adding two or more arrays to the key.
Let me know if this makes sense?
Answer
Solution:
in your initial array, make the values empty arrays:
then when you assign, just push on the array
just make sure you always treat the
$timearray
indexes as arrays, even if they only contain one (or zero!) events.good luck!
Answer
Solution:
Just make every entry in your interval array an array
Answer
Solution:
try this:
...
and
...
Answer
Solution:
So instead of setting every hour to
0
, better set everything to an empty array in your initial run. Instead of checking against0
, you can then check against an array with 0 length. So basically the same. For your date setting loop, you will now, instead of creating a new array, just push to that array. Something likeshould do the job.
After that, just loop over the dates at any given time and execute them all.