Forum Moderators: coopster

Message Too Old, No Replies

Functions returning arrays

A question about accessing array members returned by a function

         

fmouse

5:01 pm on Jan 5, 2006 (gmt 0)

10+ Year Member



I can construct a function that returns an array, however accessing the array members requires an intermediate step. i.e.:

$foo = some_function($bar)[1];


generates an "unexpected '['" error.

If I do it like this:

$zot = some_function($bar);
$foo = $zot[1];


then all is well.

The first construction would work in C or Python, but not in PHP. I seem to recall that there's an alternate syntax for directly referencing an array member of a function return, but for the life of me I can't find it on the PHP doc website. I believe I even used it years ago, but it's been a long time. Does anyone know how this is done?

coopster

8:12 pm on Jan 5, 2006 (gmt 0)

WebmasterWorld Administrator 10+ Year Member



Welcome to WebmasterWorld, fmouse.

Thought I had done that once too but if I had I certainly couldn't find it. Maybe it was in a different programming language and it just looked so familiar I thought I had done it in PHP ;-)
Anyway, you could always:

$foo = array_slice [php.net](some_function($bar),1,1);