By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
424,988 Members | 1,017 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 424,988 IT Pros & Developers. It's quick & easy.

Function returning 2 arrays - which way to do so?

P: n/a
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
Jan 7 '08 #1
Share this Question
Share on Google+
8 Replies


P: n/a
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
Jan 7 '08 #2

P: n/a
..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
Jan 7 '08 #3

P: n/a
On Jan 7, 11:15*am, Erwin Moller
<Since_humans_read_this_I_am_spammed_too_m...@spam yourself.comwrote:
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.
Jan 7 '08 #4

P: n/a
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´
Jan 7 '08 #5

P: n/a
On 7 Jan, 09:15, Erwin Moller
<Since_humans_read_this_I_am_spammed_too_m...@spam yourself.comwrote:
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.
<snip>
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
<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.
/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.
Jan 7 '08 #6

P: n/a
On Jan 7, 2:34 am, Michael Fesser <neti...@gmx.dewrote:
.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.
Jan 7 '08 #7

P: n/a
Logos <ty*********@gmail.comwrote:
>
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, ti**@probo.com
Providenza & Boekelheide, Inc.
Jan 8 '08 #8

P: n/a
On Jan 7, 11:23 pm, Tim Roberts <t...@probo.comwrote:
Logos <tyler.st...@gmail.comwrote:
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.
Jan 9 '08 #9

This discussion thread is closed

Replies have been disabled for this discussion.