Problem with Function in Araay

Reply

Join Date: Aug 2007
Posts: 130
Reputation: hemgoyal_1990 is an unknown quantity at this point 
Solved Threads: 8
hemgoyal_1990's Avatar
hemgoyal_1990 hemgoyal_1990 is offline Offline
Junior Poster

Problem with Function in Araay

 
0
  #1
Nov 1st, 2009
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.
http://www.kuchamancity.com
Hem Web Solution..
Behind Every Successful Man, There is an Untold Pain in His Heart.
Reply With Quote Quick reply to this message  
Join Date: May 2008
Posts: 525
Reputation: Will Gresham is on a distinguished road 
Solved Threads: 86
Sponsor
Will Gresham's Avatar
Will Gresham Will Gresham is offline Offline
Posting Pro
 
0
  #2
Nov 1st, 2009
To add an item to an array, do this:
  1. $array_name[] = 'Value';

Then to print out the entire array:
  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.
AJAX is not a programming language, scripting language or any other sort of language.
It is acheived by using JavaScript http functions.
So, AJAX = JavaScript.
Reply With Quote Quick reply to this message  
Join Date: May 2007
Posts: 455
Reputation: Atli is on a distinguished road 
Solved Threads: 56
Atli's Avatar
Atli Atli is offline Offline
Posting Pro in Training
 
0
  #3
Nov 1st, 2009
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.
  1. $myArray = array();
  2. function setValue($value) {
  3. if($value < 100) {
  4. global $myArray;
  5. $myArray[] = $value;
  6. setValue($value+1);
  7. }
  8. }
  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.
Please do not ask for help in a PM. Use the forums.
And use [code] tags!
Reply With Quote Quick reply to this message  
Reply

Tags
array, function, php

Message:



Other Threads in the PHP Forum


Views: 606 | Replies: 2
Thread Tools Search this Thread



Tag cloud for array, function, php
About Us | Contact Us | Advertise | DaniWeb | Acceptable Use Policy | RSS Feed

©2003 - 2009 DaniWeb® LLC