php - Adding multiple arrays to same key

418

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?

398

Answer

Solution:

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!

766

Answer

Solution:

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

Answer

Solution:

try this:

Array
(
   [0600] => array()

...

and

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

...

422

Answer

Solution:

So instead of setting every hour to0, 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 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.

People are also looking for solutions to the problem: php - Use all images from my default store on my new store view

Source

Didn't find the answer?

Our community is visited by hundreds of web development professionals every day. Ask your question and get a quick answer for free.

Ask a Question

Write quick answer

Do you know the answer to this question? Write a quick response to it. With your help, we will make our community stronger.

Similar questions

Find the answer in similar questions on our website.