0

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?

4
  • What do you mean? Commented Aug 29, 2016 at 18:15
  • 1
    change $timeArray[date('Hi', $roundStart)] = to $timeArray[date('Hi', $roundStart)][] = . that way it will have array of events in each time key. Commented Aug 29, 2016 at 18:17
  • @kcroake88 Are you looking for multiple mappings for a given key? AKA one to many Commented Aug 29, 2016 at 18:17
  • You could initialize the time array values as arrays, then use array_push() to add the event arrays inside of the array stored for the key. Commented Aug 29, 2016 at 18:17

4 Answers 4

3

in your initial array, make the values empty arrays:

Array
(
    [0600] => []
    ...etc etc
    [2300] => []
)

then when you assign, just push on the array

$timearray[$time][] = $event;

just make sure you always treat the $timearray indexes as arrays, even if they only contain one (or zero!) events.

good luck!

Sign up to request clarification or add additional context in comments.

1 Comment

That.... makes total sense. Didn't even think of that. I'll try this out.
0

Just make every entry in your interval array an array

// instead the if condition you can initialize the array in your loop when creating it
if (!is_array($timeArray[date('Hi', $roundStart)])) {
    $timeArray[date('Hi', $roundStart)] = array();
}
$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
);

Comments

0

try this:

Array
(
   [0600] => array()

...

and

$timeArray[date('Hi', $roundStart)][] = array(
   'e_name' => $eventName,
   'e_build_id' => $buildId

...

Comments

0

So instead of setting every hour to 0, better set everything to an empty array in your initial run. Instead of checking against 0, 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 like

array_push($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
));

should do the job.

After that, just loop over the dates at any given time and execute them all.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.