Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 3316882
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 17, 20262026-05-17T22:31:18+00:00 2026-05-17T22:31:18+00:00

I am looping through an array, and for each value, I need to insert

  • 0

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.

How can this be done?

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-17T22:31:19+00:00Added an answer on May 17, 2026 at 10:31 pm

    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.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

No related questions found

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.