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.










share|improve this question









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 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














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.










share|improve this question









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 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












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.










share|improve this question









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






share|improve this question









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.











share|improve this question









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.









share|improve this question




share|improve this question








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 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












  • 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







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












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=>).






share|improve this answer




















    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',
    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
    );



    );






    KameronEX is a new contributor. Be nice, and check out our Code of Conduct.









     

    draft saved


    draft discarded


















    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






























    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=>).






    share|improve this answer
























      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=>).






      share|improve this answer






















        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=>).






        share|improve this answer












        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=>).







        share|improve this answer












        share|improve this answer



        share|improve this answer










        answered 2 days ago









        Armali

        6,76793595




        6,76793595




















            KameronEX is a new contributor. Be nice, and check out our Code of Conduct.









             

            draft saved


            draft discarded


















            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.













             


            draft saved


            draft discarded














            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














































































            Popular posts from this blog

            Kleinkühnau

            Makov (Slowakei)

            Deutsches Schauspielhaus