The question you ask can be answered by asking two questions:
- Is this doable?
- Is it a good idea to do it this way?
Is this doable?
Yes! You do not need to save the array in a variable and reuse it later.
For example, you can do:
$imageAlt = get_field('image_field')['alt'];
Note. This will work in PHP 5.4+ and will be called: Parse an array parsing .
But this is not the only consideration ...
Is this good to do?
No. In many cases, this is not a good idea. The get_field() function, depending on your context, probably does a lot of work, and each time you call it, the same job is superimposed several times.
Let's say you use the count() function. It will count the number of elements in the array. To do this, he must iterate over all the elements to get the value.
If you use the count() function every time you need to check the number of elements in an array, you do the counting task every time. If you have 10 elements in your array, you probably won't notice. But if you have thousands of elements in your array, this can cause a delay problem to compute your code (aka will be slow).
This is why you would like to do something like: $count = count($myArray); and use the variable instead of calling the function.
The same goes for your question.
source share