|
Posted by Erwin Moller on 01/07/08 09:15
jodleren wrote:
> Hi!
>
> I have a function, a part of my code which I can use as a function.
I have no clue what that sentence means. :P
It
> will return 2 arrays, and I am wondering what way to do so. Both
> arrays hold strings, there are no special keys.
>
> 1) setting the arrays as globals
> 2) returnin an array of arrays
> 3) returning a large array with a known marker to indicate when the
> 2nd part starts.
>
> Any other ideas?
> As of now, it will only be used in one place... but that might change.
>
> WBR
> Sonnich
I would go with something like 2, because it is the most structured
approach in my opinion.
Something like this:
function gimme2(){
$arr1 = array("hi","bla");
$arr2 = array("more","of","this");
$returnThis = array();
$returnThis["arr1"] = $arr1;
$returnThis["arr2"] = $arr2;
return $returnThis;
}
// from code:
$result = gimme2();
// $result["arr1"] now contains first array, $result["arr2"] the second
Of course you can put the resulting arrays into a new var if that is
convenient, eg:
$result1 = $result["arr1"];
$result2 = $result["arr2"];
// unset $result maybe if not needed anymore
unset($result);
I prefer returning complex datastructures as hashed arrays, because I
can be more descriptive (like what holds what), but that is a matter of
taste.
Regards,
Erwin Moller
[Back to original message]
|