RE: [PHP] Speaking of Global Annoyance
| From: | Lars Torben Wilson | Date: | Tue, 19 Dec 2000 11:04:06 +0000 |
| Subject: | RE: [PHP] Speaking of Global Annoyance | ||
| References: | 1 | Groups: | php.general |
| Request: | Send a blank email to php-general+get-30980@lists.php.net to get a copy of this message | ||
Jonathan Sharp writes:
> Well...you're correct, you can return them as long as you have a fixed
> number of elements...but if you have a function that returns an array with n
> elements...it's quite hard to use the list() function...it's not quite as
> easy as what I said before...
>
> function ReturnArray(){
> $Array = array(n, n1, nn);
> return $Array;
> }
>
> $Array = ReturnArray();
>
> That won't work...so if you're not returning a fixed number of elements,
> it's a pain in the dajubuiti...
>
> -Jonathan
Say what? The following works for me. Returning arrays is something
I've had code doing for a long time. Why do you think it doesn't work?
<?php
error_reporting(E_ALL);
mt_srand((double) microtime() * 1000000);
function foo() {
$num_elems = mt_rand(1, 20);
$retval = array();
for ($i = 0; $i < $num_elems; $i++) {
$retval[] = "Dummy element: $i";
}
return $retval;
}
$arr = foo();
echo "Got " . count($arr) . " elements back.\n";
while (list($key, $val) = each($arr)) {
echo "Key: $key; Value: $val\n";
}
?>
> -----Original Message-----
> From: php3@developersdesk.com [mailto:php3@developersdesk.com]
> Sent: Monday, December 18, 2000 6:44 PM
> To: Jonathan Sharp; php-general@lists.php.net
> Subject: RE: [PHP] Speaking of Global Annoyance
>
>
> Addressed to: "Jonathan Sharp" <webdev@flyerware.com>
> <php-general@lists.php.net>
>
> ** Reply to note from "Jonathan Sharp" <webdev@flyerware.com> Mon, 18 Dec
> 2000 15:51:19 -0800
> >
> > but it's kinda hard to
> > return arrays since you can't just do "return $thearray;" and then on
> > the other end "$anarray = somefunction();"
>
>
> Yes you can.
>
>
> ------------------------------------------------
> <?
>
> function ReturnArray() {
>
> $Array = array( 1, 2, 3 );
> return $Array;
>
> }
>
> $AnotherArray = ReturnArray();
>
> list( $One, $Two, $Three ) = $AnotherArray;
>
>
> echo "One: $One Two: $Two Three: $Three<BR>\n";
>
>
> ?>
> ------------------------------------------------
>
>
> Returns:
>
> One: 1 Two: 2 Three: 3
>
>
>
> function ReturnArray() {
>
> return array( 1, 2, 3 );
>
> }
>
> would do the same thing.
>
>
>
>
> function ReturnComplex() {
>
>
> return array( array( 'Entry[1][1]',
> 'Entry[1][2]',
> 'Entry[1][3]' ),
> array( 'Entry[2][1]',
> 'Entry[2][2]',
> 'Entry[2][3]' ),
> array( 'Entry[3][1]',
> 'Entry[3][2]',
> 'Entry[3][3]' ));
> }
>
> will work too.
>
>
>
> $ReturnValue = ReturnComplex();
>
> echo $ReturnValue[2][3]; // Entry[2][3]
>
>
>
>
>
>
> Rick Widmer
> Internet Marketing Specialists
> http://www.developersdesk.com
>
>
>
> --
> PHP General Mailing List (http://www.php.net/)
> To unsubscribe, e-mail: php-general-unsubscribe@lists.php.net
> For additional commands, e-mail: php-general-help@lists.php.net
> To contact the list administrators, e-mail: php-list-admin@lists.php.net
>
--
+----------------------------------------------------------------+
|Torben Wilson <torben@php.net> Netmill iTech|
|http://www.coastnet.com/~torben http://www.netmill.fi|
|Ph: 1 250 383-9735 torben@netmill.fi|
+----------------------------------------------------------------+