ALL :: ZONES :: Returning Arrays from a Function |
|||
| By: digitaltree |
Date: 31/01/2003 00:00:00 |
Points: 30 | Status: Answered Quality : Excellent |
|
Hi All I need to know how to return an array from a function. Below is a class that I'm using one function sets the array the second should return the array, but current() gives and error! <?php class TEST { var $myArray = array(); function setArray($value) { array_push($this->myArray, $value); } function getArray() { return $this->myArray; } } $myObj = new $TEST; for ($a=0; $a==100; $a++) { $myObj->setArray($a); } $theArray = $myObj->getArray(); for ($a=0; $a==100; $a++) { echo(current($theArray)); // Error occurs here! } ?> |
|||
| By: VGR | Date: 31/01/2003 20:08:00 | Type : Answer |
|
| returning array pose no problem at all. Do it like this : --main script $myArray=array(); $myArray=letsCall(); --anywhere you define the function letsCall() function letsCall() { $locarray=array(); // populate $locarray return($locarray); } a better solution is to use passed-by-reference (Var) parameters, like : --main script $myArray=array(); $returnvalue=letsCall($myArray); --anywhere you define the function letsCall() function letsCall(&$par) { // Var parameter $resultat=TRUE; // populate $par as array, $resultat=FALSE on errors return($resultat); } I guess you can adapt this to your OO-style |
|||
| By: Hatemben | Date: 31/01/2003 20:25:00 | Type : Comment |
|
| Hello, First : $myObj = new TEST; Second, $a==100 will be false, and the for statement will exit withou setting any array value : DON'T DO for ($a=0; $a==100; $a++) { $myObj->setArray($a); } DO: for ($a=0; $a<=100; $a++) { $myObj->setArray($a); } And finally to print an array values use foreach($theArray as $key=>$val) { echo $val; } And combining those your script will be : <?php class TEST { var $myArray = array(); function setArray($value) { array_push($this->myArray, $value); } function getArray() { return $this->myArray; } } $myObj = new TEST; for ($a=0; $a<=100; $a++) { $myObj->setArray($a); } $theArray = $myObj->getArray(); foreach($theArray as $key=>$val) { echo $val; } ?> |
|||
| By: waygood | Date: 31/01/2003 21:19:00 | Type : Comment |
|
| as an addition to the correct answer above print_r($your_array); This is a quick coding way to display the contents of an array for debugging. It's a little slow but you don't have to worry about multidimensional arrays, as this will display any structure and it will show the indexes/keys. |
|||
| By: RQuadling | Date: 31/01/2003 21:35:00 | Type : Comment |
|
| In addition to waygood's comment. If you are using print_r in an already populated HTML page, add echo "<pre>"; print_r($array); echo "</pre>"; Otherwise the output will all be written as 1 line in the HTML page as print_r does not include HTML formatting for line breaks. You can't use nl2br in print_r as the return value from print_r is an integer and not the array dump. Richard. |
|||
| By: VGR | Date: 31/01/2003 22:03:00 | Type : Comment |
|
| Althought I never use print_r(), I learned something Richard ;-) many thanks 8-) |
|||
| By: RQuadling | Date: 31/01/2003 22:10:00 | Type : Comment |
|
| Some other things which may help. / Get cookies if (isset($_COOKIE)) { $Cookie = $_COOKIE; $Cookie["COOKIE"] = "COOKIE"; } if (isset($HTTP_COOKIE_VARS)) { $Cookie = $HTTP_COOKIE_VARS; $Cookie["COOKIE"] = "HTTP_COOKIE_VARS"; } // Get Post if (isset($_POST)) { $Post = $_POST; $Post["POST"] = "POST"; } if (isset($HTTP_POST_VARS)) { $Post = $HTTP_POST_VARS; $Post["POST"] = "HTTP_POST_VARS"; } // Get GET if (isset($_GET)) { $Get = $_GET; $Get["GET"] = "GET"; } if (isset($HTTP_GET_VARS)) { $Get = $HTTP_GET_VARS; $Get["GET"] = "HTTP_GET_VARS"; } if (isset($Get["DEBUG"]) && ($Get["DEBUG"] == "Yes")) { echo "<pre>"; print_r($Get); print_r($Post); print_r($Cookie); echo "</pre>"; } Allows you to write old and new PHP code in the same script and handle all the data coming in without worrying. So nearly all my scripts I can add &DEBUG=Yes to the end and get a whole set of debugs (only a short example shown above!). Richard. |
|||
|
Do register to be able to answer |
|||
©2010 These pages are served without commercial sponsorship. (No popup ads, etc...). Bandwidth abuse increases hosting cost forcing sponsorship or shutdown. This server aggressively defends against automated copying for any reason including offline viewing, duplication, etc... Please respect this requirement and DO NOT RIP THIS SITE.
Please DO link to this page!








