# Function returning 2 arrays - which way to do so?

#### Do you have a question? Post it now! No Registration Necessary.  Now with pictures!

•  Subject
• Author
• Posted on
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?

jodleren wrote:

I have no clue what that sentence means. :P

It

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?

On Jan 7, 11:15 am, Erwin Moller

[snip]

I tend to agree.

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

On 7 Jan, 09:15, Erwin Moller

<snip>

<snip>

/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?

.oO(jodleren)

No.

Possible.

No.

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?

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?

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?

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.

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

jodleren wrote:

Serialized in separated string.
Hmm ^^'

-thib´