Re: Re: [BULK] [PHP] Use array returned by function directly?

From: Date: Mon, 29 Jan 2007 15:27:21 +0000
Subject: Re: Re: [BULK] [PHP] Use array returned by function directly?
References: 1 2  Groups: php.general 
Request: Send a blank email to php-general+get-247942@lists.php.net to get a copy of this message
# clive@rttc.co.za / 2007-01-29 16:07:39 +0200: > ivailo91@gmail.com wrote: > >What's the best way to achieve something like this: > >explode($needle, $array)[3] > >It's too clumsy to use temporary array, and I suppose, quite slow. Is > >there any option, or I'll have to stick to temporary arrays? > > Not sure what you are trying to do. > > 1. Are you trying to explode the values of an array.(doesn't make sense > I know) > > 2. Search for a value in a array and then return them as a new array - > there are other php function for this. > > Please explain more clearly what your intentions are? The intention is clear: <?php function returns_array() { return array_flip(array('a', 'b', 'c', 'd')); } $a = returns_array(); assert(2 == $a['c']); # crash #assert(2 == returns_array()['c']); ?> Before anyone starts the "but this is not the PHP way" tirade, PHP already does this with property access to objects: <?php function returns_arrayobject() { return new ArrayObject( returns_array(), ArrayObject::ARRAY_AS_PROPS ); } $a = returns_arrayobject(); assert(2 == $a['c']); assert(2 == returns_array()->c); # still crash #assert(2 == returns_array()['c']); ?> -- How many Vietnam vets does it take to screw in a light bulb? You don't know, man. You don't KNOW. Cause you weren't THERE. http://bash.org/?255991

« previous php.general (#247942) next »