Make WordPress Core

Opened 12 years ago

Closed 12 years ago

Last modified 12 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's profile 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)

#1 follow-up: @wonderboymusic
12 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

#2 in reply to: ↑ 1 @gkb6891
12 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 12 years ago by gkb6891 (previous) (diff)

#3 @markoheijnen
12 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.

#4 follow-up: @scribu
12 years ago

This is no longer a problem as of PHP 5.4.

#5 in reply to: ↑ 4 @gkb6891
12 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.