WordPress.org

Make WordPress Core

Opened 3 years ago

Closed 3 years ago

Last modified 3 years ago

#21556 closed enhancement (wontfix)

Function to return value of an array element taking the array and a key as input

Reported by: gkb6891 Owned by:
Milestone: Priority: normal
Severity: normal Version:
Component: General Keywords:
Focuses: 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: @wonderboymusic3 years ago

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

comment:2 in reply to: ↑ 1 @gkb68913 years 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. :)

Last edited 3 years ago by gkb6891 (previous) (diff)

comment:3 @markoheijnen3 years 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.

comment:4 follow-up: @scribu3 years ago

This is no longer a problem as of PHP 5.4.

comment:5 in reply to: ↑ 4 @gkb68913 years ago

Replying to scribu:

This is no longer a problem as of PHP 5.4.

Awesome!

Note: See TracTickets for help on using tickets.