Meta Code
Meta Code

Reputation: 607

Where's the loop?

I have this code:

function filterVencimientos ($arrayFull) {
    $filtered = array_filter($arrayFull, 'month');
    var_dump($filtered);
    return $filtered;
}

function month ($var) {
    $response = false;
    if (is_array($var)) {
        foreach ($var as $item) {
            $response = month($var);
        }
    } else {
        $date = date('Y-m');
        $response = (stripos($var, $date) !== false);
    }
    return $response;
}

function getFiltered () {
    $arrayFull = getVencimientosPorPerfil();
    $arrayFiltered = filterVencimientos($arrayFull);
    return $arrayFiltered;
}

And that returns this:

Fatal error: Out of memory (allocated 4194304) (tried to allocate 262144 bytes) in C:\xampp\htdocs\app\miramonteapp\api\yscript.php on line 244

I'm trying to go through this array:

array(1) {
  [258]=>
  array(9) {
    ["recaudacion"]=>
    array(13) {
      [0]=>
      string(10) "2017-01-07"
      [1]=>
      string(10) "2017-02-07"
    }
    ["Contribuyentes Convenio Multilateral"]=>
    array(13) {
      [0]=>
      string(10) "2017-01-13"
      [1]=>
      string(10) "2017-02-13"
    }
  }
}

Where's the loop that's causing the out of memory error?

Upvotes: 1

Views: 54

Answers (1)

Ethan
Ethan

Reputation: 4375

Your error is inside the foreach in the month() function:

$response = month($var);

You should be running the month() function on the $item variable.

Here's what the poor compiler is trying to do:

  1. month() is called with an array as $var
  2. Loops through all the elements of $var
  3. Calls month() with the exact same argument as was passed in.
  4. month() is called... etc.

Step 4 is the same as step 1, so the compiler valiantly keeps trying to run your code, which is just running the same function over and over again :'(

Upvotes: 1

Related Questions