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.

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.

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.

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.