-1

I create such function for find next element from my array

protected function getProject($array, $slug, $next = 999)
{
    foreach($array as $key => $project)
    {
        if($project->getSlug() == $slug) {
            return $this->getNextProject($array, $slug, $key + 1);
        }
    }
}

But in result i have error "Maximum function nesting level of '100' reached, aborting". I know that there is a getSlug() isset. Please, help me solve this problem

  • 1
    check this link http://stackoverflow.com/questions/8656089/solution-for-fatal-error-maximum-function-nesting-level-of-100-reached-abor – Toretto Aug 27 '15 at 14:32
  • @Toretto do you think my logic is correct? – asdasd asdasd Aug 27 '15 at 14:33
  • if i var_dump($next) i get `int 999 int 2 int 2 int 2 int 2 int 2 .... ` It's correct but when $key =2 , it must return $project->getSlug(); – asdasd asdasd Aug 27 '15 at 14:36
  • 1
    Pretty sure you have an infinite loop. You are never changing the value of `$slug` (or the `$array`), so when you run this, you're gonna loop the `$array`, and find an element where `$project->getSlug() == $slug`. Then you're gonna call `getNextProject` again and run the loop again and call `getNextProject` again... and so on. You need to re-think your logic. Recursion only works when you can actually stop at one point. – gen_Eric Aug 27 '15 at 14:36
  • You have multiple return statement which is not correct. – Toretto Aug 27 '15 at 14:38
  • @Toretto: How is that "not correct"? – gen_Eric Aug 27 '15 at 14:40
  • @RocketHazmat can you help me? – asdasd asdasd Aug 27 '15 at 14:42

3 Answers3

0

Increase the limit of xdebug in php.ini
For example, making nesting level limit to 300: xdebug.max_nesting_level=300

You can also comment out this line in php.ini to remove limiting constrain: ";xdebug.max_nesting_level"

0

You have an infinite loop/infinite recursion going on here.

foreach($array as $key => $project)
{
    if($project->getSlug() == $slug) {
        return $this->getNextProject($array, $slug, $key + 1);
    }

    // ...
}

If there are any $array elements that match the condition $project->getSlug() == $slug, it will call getNextProject() again, which will run this foreach again thus calling getNextProject() again... and so on.

You need to reorder/re-think your logic here. You can try to move the if($next != 999) { to the top of the foreach.

Try this:

protected function getNextProject($array, $slug, $next = 999)
{
    foreach($array as $key => $project)
    {
        if($next != 999 && $next == $key) {
            return $project->getSlug();
        }
        if($project->getSlug() == $slug) {
            return $this->getNextProject($array, $slug, $key + 1);
        }
    }
}

Or better yet, you can just do this with one loop and no recursion:

protected function getNextProject($array, $slug)
{
    foreach($array as $key => $project)
    {
        if($project->getSlug() == $slug && isset($array[$key+1])){
            return $array[$key+1]->getSlug();
        }
    }
}

Just find the 1st element that matches the $slug, then get the next one and return its slug.

gen_Eric
  • 223,194
  • 41
  • 299
  • 337
0

I think the best way it's create new function, witch look like this:

public function yourAction($array, $key)
{
    foreach($array as $index => $project) {
        if($index == $key) {
            return $project->getProduct();
        }
    }
}

And then usa like

if($project->getProduct() == $slug) {
    $nextProduct = $this->yourAction($array, $key + 1)
    return $nextProject;
}
Bogdan Ivanov
  • 202
  • 1
  • 3
  • 11
  • Couldn't your 1st loop just be replaced with `return $array[$key]->getProduct();`? Why do you need a loop? Also, didn't he want the *next* element? – gen_Eric Aug 27 '15 at 14:56