Search code examples
phparraysmultidimensional-arrayarray-push

PHP Pushing at the end of multidimensional array


So I am trying to do the following. I have an array that contains 12 numbers(months).

$array = array();
$array[1] = array();

and so on.

Now, I am trying to push numbers into second place like $array[5][day should be pushed here] trying like so

$counter = 0;
while($counter <= $elements)
{
if($something == $something)
{
    $startfrom = 14; //example
    $month = 5; //example
    while($startfrom <= 20)
    {
    array_push($array[$month], $startfrom );
    $startfrom ++;
    }
}

But its always returning an error. Like this Fatal error: Allowed memory size of 134217728 bytes exhausted (tried to allocate 36 bytes) in F:\xampp\htdocs\NewPassword\Testt\process_info.php on line 27

What I am trying to achieve is that each day goes to its responding month.

Thanks.

Edit: Nwm, fixed I did biggest rookie mistake I could. I wasn't incrementing counter out of if statement... Thank you APerson.


Solution

  • Should be:

    $counter = 0;
    while($counter <= $elements)
    {
        if($something == $something)
        {
            $startfrom = 14; //example
            $month = 5; //example
            while($startfrom <= 20)
            {
                array_push($array[$month], $startfrom );
                $startfrom ++;
            }
        $counter++;
    }