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

Feel me, I'm learning. I often see fragments like the ones below:

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

It is pedagogical and clear and organized. But do I need to get the whole array before querying the array for values? Can I define a variable in only one line? Something like below (which doesn't work, nor other options I tried):

 $imageAlt = get_field('image_field', ['alt']); $imageURL = get_field('image_field', ['url']); 
+5
source share
4 answers

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.

+6
source

Yes, you can.

As with PHP 5.4, you can massage the dereferencing of the result of a function or calling a method directly. Prior to this, only the use of a temporary variable was possible. - Source

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

https://eval.in/548036

+7
source

While PHP 5.4+ allows you to directly dereference the return value of a function as follows:

 get_field('image_field')['alt'] 

... in this particular case, I would not suggest that you do this, since you are using two values ​​from the resulting array. A function call has certain overheads on its own, and besides, you don’t know what the function does behind the scenes before it returns the result. If you call this function twice, you can suffer a ton of unnecessary work in which the same function call would perform exactly the same.

This does not mean saving your DRY code; if you need to change the data of a function call, now you need to change it twice ...

+2
source

PHP allows you to play quite a lot:

  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 select 1 , and on the next line the same thing. Therefore, it is better to save the result of the function in a variable so that you can use it later.

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 and extract constructor.

0
source

Source: https://habr.com/ru/post/1246479/


All Articles