0

Hi all,

I Have a Problem in my Function. I am created a Recursion Function in PHP and also Created a Array.
But I Have a Trouble That How to Store This Recursion Function Value in Array.

Please Help Me I Want to Store Recursion Function Value in PHP and Print it Our of Loop.

Please Help.

Thanx.

3
Contributors
2
Replies
3
Views
7 Years
Discussion Span
Last Post by Atli
0

To add an item to an array, do this:

$array_name[] = 'Value';

Then to print out the entire array:

foreach($array_name as $key => $value) {
  echo $value . "<br />";
}

If that is not what you want then post an example of your code.

0

Hey.

To add to an array from withing a recursive function, you need to either import a global variable into the function, or pass it as a reference parameter.

$myArray = array();
function setValue($value) {
  if($value < 100) {
    global $myArray;
    $myArray[] = $value;
    setValue($value+1);
  }
}
function setValue($value, &$myArray) {
  if($value < 100) {
    $myArray[] = $value;
    setValue($value+1);
  }
}

$myArray = array();
setValue(1, $myArray);

I would recommend the second option, because it gives you more control over how the variables are handled.

If this is not what you are talking about, please explain this better. Code examples are also very helpful.

This topic has been dead for over six months. Start a new discussion instead.
Have something to contribute to this discussion? Please be thoughtful, detailed and courteous, and be sure to adhere to our posting rules.