note 36407 deleted from ref.array by didou
| From: | didou@php.net | Date: | Fri, 10 Oct 2003 12:18:42 +0000 |
| Subject: | note 36407 deleted from ref.array by didou | ||
| References: | 1 | Groups: | php.notes |
| Request: | Send a blank email to php-notes+get-58190@lists.php.net to get a copy of this message | ||
Note Submitter: php.at.keithtyler.com
----
It seems that there is no short way to extract a single value (or slice of values) from an
associative array that is returned by a function, without having to allocate an entire array to
store the function output.
For example, if f() returns an array with keys "foo", "bar", "baz",
and you want to only get the value of key "bar", the obvious (but heavy) way is:
$arr=f();
$bar=$arr["bar"];
You can't do:
$bar=(f())["bar"]; // perl syntax, or
$bar=list(f())["bar"] // php syntax of same operation
as you can in Perl.
In PHP 4.3.0, you can presumably do:
list($bar)=array_intersect_assoc(f(),array("bar" => ""))
but this is a mess, and still creates a vestigial variable, though a smalller one than the obvious
method.
One heady way to do this without added symbol overhead would be to make a function like this:
function get_assoc($array, $key) {
for (;key($array)!=$key;next($array)) {}
return current($array);
}