#21556 closed enhancement (wontfix)
Function to return value of an array element taking the array and a key as input
| Reported by: |
|
Owned by: | |
|---|---|---|---|
| Priority: | normal | Milestone: | |
| Component: | General | Version: | |
| Severity: | normal | Keywords: | |
| Cc: |
Description
PHP has a syntactical issue with a construct like the following:
$value = return_array_fun()[$key];
where return_array_fun() is a function returning an array as output.
One is forced to rewrite it as
$temp_array = return_array_fun(); $value = $temp_array[$key];
It would be nice to have a function in WordPress that makes this simple. Here's the one-liner:
/**
* Get the value of an element in an array
*/
function array_get_value( $array, $key ) {
return $array[$key];
}
I didn't create a separate diff file for this.
Change History (5)
comment:1
follow-up:
↓ 2
wonderboymusic — 9 months ago
Replying to wonderboymusic:
I don't think you need an accessor function like that, it's too much reflection.
Agreed. Maybe I should be talking to the guys at php.net project to resolve the syntactic issues. :)
comment:3
markoheijnen — 9 months ago
- Keywords has-patch removed
- Milestone Awaiting Review deleted
- Resolution set to wontfix
- Status changed from new to closed
I do believe some recent versions of PHP do allow this but as wonderboymusic said this is sure not preferable.
Note: See
TracTickets for help on using
tickets.

I don't think you need an accessor function like that, it's too much reflection. Reading values is like 1000x's faster than reading the return value of a function call in PHP. And instead of setting $value = $arr[$key], just refer to it as $arr[$key] unless you act on it by calling trim() or something