Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Can you get value from an array without getting the array first?

Tags:

arrays

php

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']);
like image 397
pastic Avatar asked Apr 05 '16 15:04

pastic


3 Answers

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

like image 162
ʰᵈˑ Avatar answered Nov 02 '22 00:11

ʰᵈˑ


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.

like image 26
Maxime Avatar answered Nov 02 '22 00:11

Maxime


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...

like image 25
deceze Avatar answered Nov 02 '22 02:11

deceze