tags:

views:

35

answers:

1

Hi,

I am looping through an array, and for each value, I need to insert another array containing a few items. The below code inserts the array fine:

foreach($events as $Key => $val):

  $schedule[$Key] = array( 
                           array('event_id' => 'test', 
                                 'start_date_time' => 'test',
                                 'end_date_time'=>'test'), ));
endforeach;

And this gives me something like the below:

    Array
(
    [1287039600] => 
    [1287043200] => 
    [1287050400] => 
    [1287054000] => 
    [1287054900] => 
    [1287057600] => 
    [1287061200] => 
    [1287064800] => Array
        (
            [0] => Array
                (
                    [event_id] => 'test'
                    [start_date_time] => 'test'
                    [end_date_time] => 'test'
                )

        )

    [1287068400] => 
    [1287072000] => 
    [1287075600] => 
)

My problem is that I need to insert more than one array for each key, and if i do this, I overwrite the previous entrance.

I think I need to increment the [0] => Array value shown above.

Can anyone advise on how this can be done?

Regards, Ben.

+4  A: 

Update:

I just realized that you always will get only one "child" element per array element, as each $Key is unique in an array anyway. That means you will never have two loops with the same $Key value.
Proof: http://codepad.org/1g4Kjccc

So if you want to insert more than one array for each key, you would have to create these arrays in one loop, e.g.:

$schedule[$Key] = array(array('event_id' => 'test', 
                              'start_date_time' => 'test',
                              'end_date_time'=>'test'),
                        array('event_id' => 'test', 
                              'start_date_time' => 'test',
                              'end_date_time'=>'test')
                        );

Maybe you have to show your "source" array and to explain how you want to create entries...


Old answer: (not wrong but does not make much of a difference ;) (as long as $schedule does not already contain values!))
I think you want:

foreach($events as $Key => $val) {
  if(!isset($schedule[$Key])) {
    $schedule[$Key] = array();
  }
  $schedule[$Key][] = array('event_id' => 'test', 
                            'start_date_time' => 'test',
                            'end_date_time'=>'test');
}

You are right, that you are constantly overwriting the value... by initializing the element $schedule[$Key] as array once and by using $schedule[$Key][], you append the new value to the array.

See the PHP array manual.

Felix Kling
Thanks for the quick reply but I get the same issue. Each child array is overwritten. Regards, Ben.
Ben
@Ben: This is how it will be. I just realized that every element will contain **at most one** child element anyway, as each `$Key` is unique.
Felix Kling
@Ben: Of course this is different if `$schedule` is already prepopulated with values... but then my "old answer" should work...
Felix Kling
@Felix - Quite right. Doing it this was is impossible. I need to go through the "source" array, build a new array that contains each of the required entrances, and then insert them all together into the schedule array.
Ben
Thanks for the help, much appreciated! Regards, Ben.
Ben