耐心等待,我正在学习。 我经常看到如下片段:
<?p
$imageArray = get_field('image_field');
$imageAlt = $imageArray['alt'];
$imageURL = $imageArray['url'];
?>
它具有教学意义,条理清晰。但是在查询数组的值之前是否有必要获取整个数组?我不能只在一行中定义变量吗?类似于下面的内容(不起作用,我尝试过的其他变体也没有):
$imageAlt = get_field('image_field', ['alt']);
$imageURL = get_field('image_field', ['url']);
最佳答案
是的,你可以。
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'];
关于php - 你能在不先获取数组的情况下从数组中获取值吗?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/36431021/