How to push elements in to an array of an array with a key?
up vote
-3
down vote
favorite
<?php
$tab=array(
"January"=>array(),
"February"=>array(),
"March"=>array(),
"April"=>array(),
"May"=>array(),
"June"=>array()
);
foreach($tab as $month=>$number)
for($i=0;$i<6;$i++)
array_push($number,rand(10,20));
print_r($tab);
?>
I'm trying to push multiple numbers into an array which is inside an array where the keys are the names of months. print_r displays empty arrays. I don't know what I did wrong.
php
New contributor
KameronEX is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
Check out our Code of Conduct.
add a comment |
up vote
-3
down vote
favorite
<?php
$tab=array(
"January"=>array(),
"February"=>array(),
"March"=>array(),
"April"=>array(),
"May"=>array(),
"June"=>array()
);
foreach($tab as $month=>$number)
for($i=0;$i<6;$i++)
array_push($number,rand(10,20));
print_r($tab);
?>
I'm trying to push multiple numbers into an array which is inside an array where the keys are the names of months. print_r displays empty arrays. I don't know what I did wrong.
php
New contributor
KameronEX is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
Check out our Code of Conduct.
4
“I don't know what I did wrong.” - you did not read the manual forforeachattentively enough, because it explains what you need to do to manipulate the array you are looping over inside the loop.
– misorude
2 days ago
add a comment |
up vote
-3
down vote
favorite
up vote
-3
down vote
favorite
<?php
$tab=array(
"January"=>array(),
"February"=>array(),
"March"=>array(),
"April"=>array(),
"May"=>array(),
"June"=>array()
);
foreach($tab as $month=>$number)
for($i=0;$i<6;$i++)
array_push($number,rand(10,20));
print_r($tab);
?>
I'm trying to push multiple numbers into an array which is inside an array where the keys are the names of months. print_r displays empty arrays. I don't know what I did wrong.
php
New contributor
KameronEX is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
Check out our Code of Conduct.
<?php
$tab=array(
"January"=>array(),
"February"=>array(),
"March"=>array(),
"April"=>array(),
"May"=>array(),
"June"=>array()
);
foreach($tab as $month=>$number)
for($i=0;$i<6;$i++)
array_push($number,rand(10,20));
print_r($tab);
?>
I'm trying to push multiple numbers into an array which is inside an array where the keys are the names of months. print_r displays empty arrays. I don't know what I did wrong.
php
php
New contributor
KameronEX is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
Check out our Code of Conduct.
New contributor
KameronEX is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
Check out our Code of Conduct.
edited 2 days ago
Suraj Rao
22k75467
22k75467
New contributor
KameronEX is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
Check out our Code of Conduct.
asked 2 days ago
KameronEX
1
1
New contributor
KameronEX is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
Check out our Code of Conduct.
New contributor
KameronEX is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
Check out our Code of Conduct.
KameronEX is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
Check out our Code of Conduct.
4
“I don't know what I did wrong.” - you did not read the manual forforeachattentively enough, because it explains what you need to do to manipulate the array you are looping over inside the loop.
– misorude
2 days ago
add a comment |
4
“I don't know what I did wrong.” - you did not read the manual forforeachattentively enough, because it explains what you need to do to manipulate the array you are looping over inside the loop.
– misorude
2 days ago
4
4
“I don't know what I did wrong.” - you did not read the manual for
foreach attentively enough, because it explains what you need to do to manipulate the array you are looping over inside the loop.– misorude
2 days ago
“I don't know what I did wrong.” - you did not read the manual for
foreach attentively enough, because it explains what you need to do to manipulate the array you are looping over inside the loop.– misorude
2 days ago
add a comment |
1 Answer
1
active
oldest
votes
up vote
1
down vote
In his eloquent comment, misorude meant to refer you to this paragraph of the foreach documentation:
In order to be able to directly modify array elements within the loop precede
$value with &. In that case the value will be assigned by
reference.
So, you could change your foreach to
foreach ($tab as &$number)
(since you don't need the key in your loop, you can omit $month=>).
add a comment |
1 Answer
1
active
oldest
votes
1 Answer
1
active
oldest
votes
active
oldest
votes
active
oldest
votes
up vote
1
down vote
In his eloquent comment, misorude meant to refer you to this paragraph of the foreach documentation:
In order to be able to directly modify array elements within the loop precede
$value with &. In that case the value will be assigned by
reference.
So, you could change your foreach to
foreach ($tab as &$number)
(since you don't need the key in your loop, you can omit $month=>).
add a comment |
up vote
1
down vote
In his eloquent comment, misorude meant to refer you to this paragraph of the foreach documentation:
In order to be able to directly modify array elements within the loop precede
$value with &. In that case the value will be assigned by
reference.
So, you could change your foreach to
foreach ($tab as &$number)
(since you don't need the key in your loop, you can omit $month=>).
add a comment |
up vote
1
down vote
up vote
1
down vote
In his eloquent comment, misorude meant to refer you to this paragraph of the foreach documentation:
In order to be able to directly modify array elements within the loop precede
$value with &. In that case the value will be assigned by
reference.
So, you could change your foreach to
foreach ($tab as &$number)
(since you don't need the key in your loop, you can omit $month=>).
In his eloquent comment, misorude meant to refer you to this paragraph of the foreach documentation:
In order to be able to directly modify array elements within the loop precede
$value with &. In that case the value will be assigned by
reference.
So, you could change your foreach to
foreach ($tab as &$number)
(since you don't need the key in your loop, you can omit $month=>).
answered 2 days ago
Armali
6,76793595
6,76793595
add a comment |
add a comment |
KameronEX is a new contributor. Be nice, and check out our Code of Conduct.
KameronEX is a new contributor. Be nice, and check out our Code of Conduct.
KameronEX is a new contributor. Be nice, and check out our Code of Conduct.
KameronEX is a new contributor. Be nice, and check out our Code of Conduct.
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
StackExchange.ready(
function ()
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53225193%2fhow-to-push-elements-in-to-an-array-of-an-array-with-a-key%23new-answer', 'question_page');
);
Post as a guest
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
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
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
4
“I don't know what I did wrong.” - you did not read the manual for
foreachattentively enough, because it explains what you need to do to manipulate the array you are looping over inside the loop.– misorude
2 days ago