0

right now I have:

$image = get_field('image');

If I var_dump($image); Data returned will look like this:

Array
(
[id] => 540
[alt] => A Movie
[title] => Movie Poster: UP
[caption] => sweet image
[description] => a man and a baloon
[url] => http://localhost:8888/acf/up.jpg

)

If I want to access the title I would do:

$image['title'];

what if I want to access it directly wiithout a variable like:

get_field('image')['title'];

is it possible?

0

3 Answers 3

2

Pasted directly from the manual:

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.

As of PHP 5.5 it is possible to array dereference an array literal.

Example #7 Array dereferencing

<?php
function getArray() {
    return array(1, 2, 3);
}

// on PHP 5.4
$secondElement = getArray()[1];

// previously
$tmp = getArray();
$secondElement = $tmp[1];

// or
list(, $secondElement) = getArray();
?>

So yes - you can - but you need PHP 5.4+

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

Comments

2

As far as I know it is possible since PHP 5.4.

http://php.net/manual/en/migration54.new-features.php (check for point number 3)

All you have to do is just update version of you php :)

Comments

1

You said object, but your example is an array.

Anyway it's called array dereference, and as Jari said has been possible since php 5.4.

From https://www.php.net/manual/en/language.types.array.php

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.

As of PHP 5.5 it is possible to array dereference an array literal.

Comments

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.