note 36407 added to ref.array
| From: | php dot at dot keithtyler dot com at rn2 dot php dot net | Date: | Wed, 08 Oct 2003 20:26:36 +0000 |
| Subject: | note 36407 added to ref.array | ||
| Groups: | php.notes | ||
| Request: | Send a blank email to php-notes+get-58073@lists.php.net to get a copy of this message | ||
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);
}
----
Manual Page -- http://www.php.net/manual/en/ref.array.php
Edit -- http://master.php.net/manage/user-notes.php?action=edit+36407
Delete -- http://master.php.net/manage/user-notes.php?action=delete+36407&report=yes
Reject -- http://master.php.net/manage/user-notes.php?action=reject+36407&report=yes
Search -- http://master.php.net/manage/user-notes.php