944,106 Members | Top Members by Rank

Ad:
  • PHP Discussion Thread
  • Unsolved
  • Views: 1341
  • PHP RSS
Nov 1st, 2009
0

Problem with Function in Araay

Expand Post »
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.
Reputation Points: 18
Solved Threads: 17
Junior Poster
hemgoyal_1990 is offline Offline
175 posts
since Aug 2007
Nov 1st, 2009
0
Re: Problem with Function in Araay
To add an item to an array, do this:
PHP Syntax (Toggle Plain Text)
  1. $array_name[] = 'Value';

Then to print out the entire array:
PHP Syntax (Toggle Plain Text)
  1. foreach($array_name as $key => $value) {
  2. echo $value . "<br />";
  3. }

If that is not what you want then post an example of your code.
Reputation Points: 96
Solved Threads: 124
Master Poster
Will Gresham is offline Offline
728 posts
since May 2008
Nov 1st, 2009
0
Re: Problem with Function in Araay
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.
php Syntax (Toggle Plain Text)
  1. $myArray = array();
  2. function setValue($value) {
  3. if($value < 100) {
  4. global $myArray;
  5. $myArray[] = $value;
  6. setValue($value+1);
  7. }
  8. }
php Syntax (Toggle Plain Text)
  1. function setValue($value, &$myArray) {
  2. if($value < 100) {
  3. $myArray[] = $value;
  4. setValue($value+1);
  5. }
  6. }
  7.  
  8. $myArray = array();
  9. 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.
Reputation Points: 93
Solved Threads: 70
Posting Pro
Atli is offline Offline
526 posts
since May 2007

This thread is more than three months old

No one has posted to this discussion for at least three months. Please let old threads die and do not reply to them unless you feel you have something new and valuable to contribute that absolutely must be added to make the discussion complete. Otherwise, please start a new thread in this forum instead.
Message:
Previous Thread in PHP Forum Timeline: PHP Error on line
Next Thread in PHP Forum Timeline: send link to another server





About Us | Contact Us | Advertise | Acceptable Use Policy
Forum Index | Build Custom RSS Feed


Follow us on Twitter


© 2011 DaniWeb® LLC