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?
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
add a comment |
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
add a comment |
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
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
php arrays
edited Mar 7 at 0:57
Cœur
19k9112154
19k9112154
asked Oct 18 '10 at 20:51
BenBen
153
153
add a comment |
add a comment |
1 Answer
1
active
oldest
votes
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.
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
add a comment |
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
);
);
Sign up or log in
StackExchange.ready(function ()
StackExchange.helpers.onClickDraftSave('#login-link');
);
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
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
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.
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
add a comment |
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.
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
add a comment |
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.
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.
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
add a comment |
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
add a comment |
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.
Sign up or log in
StackExchange.ready(function ()
StackExchange.helpers.onClickDraftSave('#login-link');
);
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
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
Sign up or log in
StackExchange.ready(function ()
StackExchange.helpers.onClickDraftSave('#login-link');
);
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function ()
StackExchange.helpers.onClickDraftSave('#login-link');
);
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function ()
StackExchange.helpers.onClickDraftSave('#login-link');
);
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
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