0

I've got a little problem with recursive function output. Here's the code:

function getTemplate($id) {
    global $templates;
    $arr = $templates[$id-1];
    if($arr['parentId'] != 0) {
        $arr['text'] .= str_replace($arr['attr'], $arr['text'], getTemplate($arr['parentId']));
    }
    return $arr['text']; 
}

The problem is that that function returns a value on each iteration like this:

file.exe
category / file.exe
root / category / file.exe

And I need only the last string which resembles full path. Any suggestions?

//UPD: done, the problem was with the dot in $arr['text'] .= str_replace

10
  • 1
    what is $templates - example? Commented Sep 17, 2013 at 12:02
  • 1
    @sashkello it is sure a recursive function, because it calls itself. Commented Sep 17, 2013 at 12:03
  • 3
    if you are working with directories and files have a look at RecursiveDirectoryIterator() php.net/manual/es/class.recursivedirectoryiterator.php Commented Sep 17, 2013 at 12:03
  • @sash - a function that calls itself must be under the term recursive Commented Sep 17, 2013 at 12:03
  • This code would be much much better if it didn't use a global. There's no need for it, and it adds unnecessary complication. Commented Sep 17, 2013 at 12:05

3 Answers 3

1

Try this please. I know its using global variable but I think this should work

$arrGlobal = array();

function getTemplate($id) {
    global $templates;
    global $arrGlobal;

    $arr = $templates[$id-1];
    if($arr['parentId'] != 0) {
       array_push($arrGlobal, getTemplate($arr['parentId']));
    }
    return $arr['text'];
}

$arrGlobal = array_reverse($arrGlobal);

echo implode('/',$arrGlobal);  

Sign up to request clarification or add additional context in comments.

2 Comments

i've found the problem in my code, it was the dot in $arr['text'] .= thanks for your response, it worked too ;)
Thank you for your comment :) Can you mark my solution as useful so it may help some one else
0

Try this,

function getTemplate($id) {
    global $templates;
    $arr = $templates[$id-1];
    if($arr['parentId'] != 0) {
    return $arr['text'] .= str_replace($arr['attr'], $arr['text'], getTemplate($arr['parentId']));
    }
}

3 Comments

that function outputs the same thing without the last string which i need.
One thing I want to know, does $arr['parentId'] != 0 means it will stop iterating here ?
$arr['parentId']=0 means that this is the root folder; we don't need to go deeper and the function start replacing the text.
0

Give this a try:

function getTemplate($id, array $templates = array())
{
  $index = $id - 1;
  if (isset($templates[$index])) {
    $template = $templates[$index];
    if (isset($template['parentId']) && $template['parentId'] != 0) {
      $parent = getTemplate($template['parentId'], $templates);
      $template['text'] .= str_replace($template['attr'], $template['text'], $parent);
    }
    return $template['text'];
  }
  return '';
}

$test = getTemplate(123, $templates);

echo $test;

2 Comments

same result with my function, returns on each iteration
@bigbobr In my example $test will hold the entire string. The function will need to return each value in order to be used recursively. Please update your question showing where you are using the function. It shouldn't be used within a loop or you will overwrite the value of $test on each iteration.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.