5

Bear with me, I'm learning. I often see snippets like the one below:

<?p
$imageArray = get_field('image_field');
$imageAlt = $imageArray['alt'];
$imageURL = $imageArray['url'];
?>

It is pedagogical and clear and organized. But is it necessary to get the entire array before querying the array for values? Can I not define the variable in just a single line? Something like the below (which doesn't work, neither the other variants I have tried):

$imageAlt = get_field('image_field', ['alt']);
$imageURL = get_field('image_field', ['url']);

4 Answers 4

7

Yes, you can.

As of PHP 5.4 it is possible to array dereference the result of a function or method call directly. Before it was only possible using a temporary variable. - Source

$imageAlt = get_field('image_field')['alt'];

https://eval.in/548036

Sign up to request clarification or add additional context in comments.

Comments

6

The question you are asking can be answered by asking 2 questions:

  1. Is it doable ?
  2. Is it a good idea to do it that way ?

Is it doable ?

Yes! You do not have to store the array in a variable and re-use it later.

For instance, you could do:

$imageAlt = get_field('image_field')['alt'];

Note: This will work in PHP 5.4+ and is called: Array dereferencing.

But that is not the only consideration...

Is it a good idea to do it that way ?

No. It's not a good idea in many cases. The get_field() function, depending on your context, is probably doing a lot of work and, each time you call it, the same work is don multiple times.

Let's say you use the count() function. It will count the number of items in an array. To do that, it must iterate through all items to get the value.

If you use the count() function each time you need to validate number of items in an array, you are doing the task of counting each and every time. If you have 10 items in your array, you probably won't notice. But if you have thousands of items in your array, this may cause a delay problem to compute your code (a.k.a. it will be slow).

That is why you would want to do something like: $count = count($myArray); and use a variable instead of calling the function.

The same applies to your question.

Comments

2

While PHP 5.4+ allows you to directly dereference a function return value like this:

get_field('image_field')['alt']

...in this particular case I would not suggest you do so, since you're using two values from the resulting array. A function call has a certain overhead just in itself, and additionally you don't know what the function does behind the scenes before it returns a result. If you call the function twice, you may incur a ton of unnecessary work, where a single function call would have done just as well.

This is not to mention keeping your code DRY; if you need to change the particulars of the function call, you now need to change it twice...

Comments

0

PHP allows you to play around quite a bit:

  function foo(){
    return array('foo' => 1, 'bar' => 2);
  }

Option 1

  echo foo()['foo']; // 1

  # Better do this if you plan to reuse the array value.
  echo ($tmp = foo())['foo']; // 1
  echo $tmp['bar']; // 2

It is not recommended to call a function that returns an array, to specifically fetch 1 key and on the next line doing the same thing. So it is better to store the result of the function in a variable so you can use it afterwards.

Option 2

  list($foo, $bar) = array_values(foo()); 
   #foo is the first element of the array, and sets in $foo.
   #bar is the second element, and will be set in $bar.
   #This behavior is in PHP 7, previously it was ordered from right to left.
  echo $foo, $bar; // 12

Option 3

  extract(foo()); // Creates variable from the array keys.
  echo $foo, $bar;

  extract(get_field('image_field'));
  echo $alt, $url;

Find more information on the list constructor and extract function.

3 Comments

Do you mean $alt = ($f = get_field('image_field'));?
@ʰᵈˑ No, I did not. While I updated the code now, the result of your code is that $alt has the same value of $f, while $alt = ($f = get_field('image_field'))['alt']; correctly sets $alt to the key of the returned array where $f is the result of the function.
Understandable, it is for that reason I usually avoid chaining things like this.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.