kcroake88
kcroake88

Reputation: 29

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?

Upvotes: 0

Views: 61

Answers (4)

baao
baao

Reputation: 73241

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.

Upvotes: 0

derelict
derelict

Reputation: 2082

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!

Upvotes: 3

Mariella
Mariella

Reputation: 117

try this:

Array
(
   [0600] => array()

...

and

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

...

Upvotes: 0

Dimitri Lavrenük
Dimitri Lavrenük

Reputation: 4879

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
);

Upvotes: 0

Related Questions