Iterating through a PHP array and inserting another array without overwritingHow to check if PHP array is associative or sequential?PHP: Delete an element from an arrayWhy is using “for…in” with array iteration a bad idea?How to insert an item into an array at a specific index (JavaScript)?How to extend an existing JavaScript array with another array, without creating a new arrayLoop through an array in JavaScriptPush array items into another arrayConvert PHP object to associative arrayPHP array delete by value (not key)Loop through an array of strings in Bash?

Do native speakers use "ultima" and "proxima" frequently in spoken English?

Would this string work as string?

Travelling in US for more than 90 days

Started in 1987 vs. Starting in 1987

Would a primitive species be able to learn English from reading books alone?

Why is participating in the European Parliamentary elections used as a threat?

PTIJ: Which Dr. Seuss books should one obtain?

Are hand made posters acceptable in Academia?

What can I do if I am asked to learn different programming languages very frequently?

How to test the sharpness of a knife?

Trouble reading roman numeral notation with flats

Not hide and seek

Hashing password to increase entropy

Air travel with refrigerated insulin

Is divisi notation needed for brass or woodwind in an orchestra?

Offset in split text content

Friend wants my recommendation but I don't want to give it to him

How can a new country break out from a developed country without war?

Should a narrator ever describe things based on a character's view instead of facts?

Pre-Employment Background Check With Consent For Future Checks

"Oh no!" in Latin

Mortal danger in mid-grade literature

Can you take a "free object interaction" while incapacitated?

1 John in Luther’s Bibel



Iterating through a PHP array and inserting another array without overwriting


How to check if PHP array is associative or sequential?PHP: Delete an element from an arrayWhy is using “for…in” with array iteration a bad idea?How to insert an item into an array at a specific index (JavaScript)?How to extend an existing JavaScript array with another array, without creating a new arrayLoop through an array in JavaScriptPush array items into another arrayConvert PHP object to associative arrayPHP array delete by value (not key)Loop through an array of strings in Bash?













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?










share|improve this question




























    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?










    share|improve this question


























      0












      0








      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?










      share|improve this question
















      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?







      php arrays






      share|improve this question















      share|improve this question













      share|improve this question




      share|improve this question








      edited Mar 7 at 0:57









      Cœur

      19k9112154




      19k9112154










      asked Oct 18 '10 at 20:51









      BenBen

      153




      153






















          1 Answer
          1






          active

          oldest

          votes


















          5














          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.






          share|improve this answer

























          • Thanks for the quick reply but I get the same issue. Each child array is overwritten. Regards, Ben.

            – Ben
            Oct 18 '10 at 21:08











          • @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
            Oct 18 '10 at 21:10












          • @Ben: Of course this is different if $schedule is already prepopulated with values... but then my "old answer" should work...

            – Felix Kling
            Oct 18 '10 at 21:25











          • 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
            Oct 18 '10 at 21:50











          • Thanks for the help, much appreciated! Regards, Ben.

            – Ben
            Oct 18 '10 at 21:50










          Your Answer






          StackExchange.ifUsing("editor", function ()
          StackExchange.using("externalEditor", function ()
          StackExchange.using("snippets", function ()
          StackExchange.snippets.init();
          );
          );
          , "code-snippets");

          StackExchange.ready(function()
          var channelOptions =
          tags: "".split(" "),
          id: "1"
          ;
          initTagRenderer("".split(" "), "".split(" "), channelOptions);

          StackExchange.using("externalEditor", function()
          // Have to fire editor after snippets, if snippets enabled
          if (StackExchange.settings.snippets.snippetsEnabled)
          StackExchange.using("snippets", function()
          createEditor();
          );

          else
          createEditor();

          );

          function createEditor()
          StackExchange.prepareEditor(
          heartbeatType: 'answer',
          autoActivateHeartbeat: false,
          convertImagesToLinks: true,
          noModals: true,
          showLowRepImageUploadWarning: true,
          reputationToPostImages: 10,
          bindNavPrevention: true,
          postfix: "",
          imageUploader:
          brandingHtml: "Powered by u003ca class="icon-imgur-white" href="https://imgur.com/"u003eu003c/au003e",
          contentPolicyHtml: "User contributions licensed under u003ca href="https://creativecommons.org/licenses/by-sa/3.0/"u003ecc by-sa 3.0 with attribution requiredu003c/au003e u003ca href="https://stackoverflow.com/legal/content-policy"u003e(content policy)u003c/au003e",
          allowUrls: true
          ,
          onDemand: true,
          discardSelector: ".discard-answer"
          ,immediatelyShowMarkdownHelp:true
          );



          );













          draft saved

          draft discarded


















          StackExchange.ready(
          function ()
          StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f3963278%2fiterating-through-a-php-array-and-inserting-another-array-without-overwriting%23new-answer', 'question_page');

          );

          Post as a guest















          Required, but never shown

























          1 Answer
          1






          active

          oldest

          votes








          1 Answer
          1






          active

          oldest

          votes









          active

          oldest

          votes






          active

          oldest

          votes









          5














          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.






          share|improve this answer

























          • Thanks for the quick reply but I get the same issue. Each child array is overwritten. Regards, Ben.

            – Ben
            Oct 18 '10 at 21:08











          • @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
            Oct 18 '10 at 21:10












          • @Ben: Of course this is different if $schedule is already prepopulated with values... but then my "old answer" should work...

            – Felix Kling
            Oct 18 '10 at 21:25











          • 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
            Oct 18 '10 at 21:50











          • Thanks for the help, much appreciated! Regards, Ben.

            – Ben
            Oct 18 '10 at 21:50















          5














          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.






          share|improve this answer

























          • Thanks for the quick reply but I get the same issue. Each child array is overwritten. Regards, Ben.

            – Ben
            Oct 18 '10 at 21:08











          • @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
            Oct 18 '10 at 21:10












          • @Ben: Of course this is different if $schedule is already prepopulated with values... but then my "old answer" should work...

            – Felix Kling
            Oct 18 '10 at 21:25











          • 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
            Oct 18 '10 at 21:50











          • Thanks for the help, much appreciated! Regards, Ben.

            – Ben
            Oct 18 '10 at 21:50













          5












          5








          5







          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.






          share|improve this answer















          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.







          share|improve this answer














          share|improve this answer



          share|improve this answer








          edited Oct 18 '10 at 21:26

























          answered Oct 18 '10 at 20:53









          Felix KlingFelix Kling

          560k130869929




          560k130869929












          • Thanks for the quick reply but I get the same issue. Each child array is overwritten. Regards, Ben.

            – Ben
            Oct 18 '10 at 21:08











          • @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
            Oct 18 '10 at 21:10












          • @Ben: Of course this is different if $schedule is already prepopulated with values... but then my "old answer" should work...

            – Felix Kling
            Oct 18 '10 at 21:25











          • 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
            Oct 18 '10 at 21:50











          • Thanks for the help, much appreciated! Regards, Ben.

            – Ben
            Oct 18 '10 at 21:50

















          • Thanks for the quick reply but I get the same issue. Each child array is overwritten. Regards, Ben.

            – Ben
            Oct 18 '10 at 21:08











          • @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
            Oct 18 '10 at 21:10












          • @Ben: Of course this is different if $schedule is already prepopulated with values... but then my "old answer" should work...

            – Felix Kling
            Oct 18 '10 at 21:25











          • 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
            Oct 18 '10 at 21:50











          • Thanks for the help, much appreciated! Regards, Ben.

            – Ben
            Oct 18 '10 at 21:50
















          Thanks for the quick reply but I get the same issue. Each child array is overwritten. Regards, Ben.

          – Ben
          Oct 18 '10 at 21:08





          Thanks for the quick reply but I get the same issue. Each child array is overwritten. Regards, Ben.

          – Ben
          Oct 18 '10 at 21:08













          @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
          Oct 18 '10 at 21:10






          @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
          Oct 18 '10 at 21:10














          @Ben: Of course this is different if $schedule is already prepopulated with values... but then my "old answer" should work...

          – Felix Kling
          Oct 18 '10 at 21:25





          @Ben: Of course this is different if $schedule is already prepopulated with values... but then my "old answer" should work...

          – Felix Kling
          Oct 18 '10 at 21:25













          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
          Oct 18 '10 at 21:50





          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
          Oct 18 '10 at 21:50













          Thanks for the help, much appreciated! Regards, Ben.

          – Ben
          Oct 18 '10 at 21:50





          Thanks for the help, much appreciated! Regards, Ben.

          – Ben
          Oct 18 '10 at 21:50



















          draft saved

          draft discarded
















































          Thanks for contributing an answer to Stack Overflow!


          • Please be sure to answer the question. Provide details and share your research!

          But avoid


          • Asking for help, clarification, or responding to other answers.

          • Making statements based on opinion; back them up with references or personal experience.

          To learn more, see our tips on writing great answers.




          draft saved


          draft discarded














          StackExchange.ready(
          function ()
          StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f3963278%2fiterating-through-a-php-array-and-inserting-another-array-without-overwriting%23new-answer', 'question_page');

          );

          Post as a guest















          Required, but never shown





















































          Required, but never shown














          Required, but never shown












          Required, but never shown







          Required, but never shown

































          Required, but never shown














          Required, but never shown












          Required, but never shown







          Required, but never shown







          Popular posts from this blog

          Save data to MySQL database using ExtJS and PHP [closed]2019 Community Moderator ElectionHow can I prevent SQL injection in PHP?Which MySQL data type to use for storing boolean valuesPHP: Delete an element from an arrayHow do I connect to a MySQL Database in Python?Should I use the datetime or timestamp data type in MySQL?How to get a list of MySQL user accountsHow Do You Parse and Process HTML/XML in PHP?Reference — What does this symbol mean in PHP?How does PHP 'foreach' actually work?Why shouldn't I use mysql_* functions in PHP?

          Compiling GNU Global with universal-ctags support Announcing the arrival of Valued Associate #679: Cesar Manara Planned maintenance scheduled April 23, 2019 at 23:30 UTC (7:30pm US/Eastern) Data science time! April 2019 and salary with experience The Ask Question Wizard is Live!Tags for Emacs: Relationship between etags, ebrowse, cscope, GNU Global and exuberant ctagsVim and Ctags tips and trickscscope or ctags why choose one over the other?scons and ctagsctags cannot open option file “.ctags”Adding tag scopes in universal-ctagsShould I use Universal-ctags?Universal ctags on WindowsHow do I install GNU Global with universal ctags support using Homebrew?Universal ctags with emacsHow to highlight ctags generated by Universal Ctags in Vim?

          Add ONERROR event to image from jsp tldHow to add an image to a JPanel?Saving image from PHP URLHTML img scalingCheck if an image is loaded (no errors) with jQueryHow to force an <img> to take up width, even if the image is not loadedHow do I populate hidden form field with a value set in Spring ControllerStyling Raw elements Generated from JSP tagds with Jquery MobileLimit resizing of images with explicitly set width and height attributeserror TLD use in a jsp fileJsp tld files cannot be resolved