Function returning 2 arrays - which way to do so?

Function returning 2 arrays - which way to do so?

am 07.01.2008 07:41:28 von jodleren

Hi!

I have a function, a part of my code which I can use as a function. 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

Re: Function returning 2 arrays - which way to do so?

am 07.01.2008 10:15:06 von Erwin Moller

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

Re: Function returning 2 arrays - which way to do so?

am 07.01.2008 10:34:49 von Michael Fesser

..oO(jodleren)

>I have a function, a part of my code which I can use as a function. 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

No.

>2) returnin an array of arrays

Possible.

>3) returning a large array with a known marker to indicate when the
>2nd part starts.

No.

>Any other ideas?

4) Arguments passed by reference:

function foo(&$arg1, &$arg2) {
$arg1 = array(1, 2, 3);
$arg2 = array(4, 5, 6);
}

Micha

Re: Function returning 2 arrays - which way to do so?

am 07.01.2008 12:33:40 von jodleren

On Jan 7, 11:15=A0am, Erwin Moller
wrote:
> jodleren wrote:
> > Hi!
>
> 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.
[snip]
>
> 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.

I tend to agree.

Re: Function returning 2 arrays - which way to do so?

am 07.01.2008 12:38:12 von thyb0

jodleren wrote:
> Hi!
>
> I have a function, a part of my code which I can use as a function. 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

Serialized in separated string.
Hmm ^^'

-thibĀ“

Re: Function returning 2 arrays - which way to do so?

am 07.01.2008 13:20:45 von colin.mckinnon

On 7 Jan, 09:15, Erwin Moller
wrote:
> 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.

> 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

>
> 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.

/me prefers not to explicitly create data structures which are only
used once:

function gimme2(){
$arr1 = array("hi","bla");
$arr2 = array("more","of","this");
return array($arr1, $arr2);
}

list($first, $second)=gime2();

C.

Re: Function returning 2 arrays - which way to do so?

am 07.01.2008 15:04:30 von Logos

On Jan 7, 2:34 am, Michael Fesser wrote:
> .oO(jodleren)
>
> >I have a function, a part of my code which I can use as a function. 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
>
> No.
>
> >2) returnin an array of arrays
>
> Possible.
>
> >3) returning a large array with a known marker to indicate when the
> >2nd part starts.
>
> No.
>
> >Any other ideas?
>
> 4) Arguments passed by reference:
>
> function foo(&$arg1, &$arg2) {
> $arg1 = array(1, 2, 3);
> $arg2 = array(4, 5, 6);
>
> }
>
> Micha

I'll second this one. Passing in both arrays by reference is clean &
tidy, and a commonly accepted way for a function to 'return' more than
one value.

Re: Function returning 2 arrays - which way to do so?

am 08.01.2008 07:23:02 von Tim Roberts

Logos wrote:
>
>I'll second this one. Passing in both arrays by reference is clean &
>tidy, and a commonly accepted way for a function to 'return' more than
>one value.

That's true in C and C++, but only because there's no way to return several
things at once.

There are two issues with the reference solution. First, it requires the
caller to allocate arrays first, and then pass those arrays to the
function. In many cases, that's not natural. Second, it creates a high
degree of linkage between the callee and caller, which again may not be
natural.

Now, these are both highly philosophical issues, but there is good sense in
adapting a policy that things to be returned should always be "returned",
rather than by modifying in/out parameters.
--
Tim Roberts, timr@probo.com
Providenza & Boekelheide, Inc.

Re: Function returning 2 arrays - which way to do so?

am 09.01.2008 18:55:11 von Logos

On Jan 7, 11:23 pm, Tim Roberts wrote:
> Logos wrote:
>
> >I'll second this one. Passing in both arrays by reference is clean &
> >tidy, and a commonly accepted way for a function to 'return' more than
> >one value.
>
> That's true in C and C++, but only because there's no way to return several
> things at once.
>
> There are two issues with the reference solution. First, it requires the
> caller to allocate arrays first, and then pass those arrays to the
> function. In many cases, that's not natural. Second, it creates a high
> degree of linkage between the callee and caller, which again may not be
> natural.
>
> Now, these are both highly philosophical issues, but there is good sense in
> adapting a policy that things to be returned should always be "returned",
> rather than by modifying in/out parameters.
> --
> Tim Roberts, t...@probo.com
> Providenza & Boekelheide, Inc.

Well, I'll admit to a C background.

However, if you're going to offer a critique I think it's only fair to
report which solution you think best & why, no? We have several
options for things being returned :)

IMHO, I think this is one of those stylistic things that really
doesn't have a best answer. It doesn't really impact performance in
any way - all that it impacts is *how the coder* interacts with the
code.