使用PHP数组中的最后一项


Using the last item in a PHP array

你好,我正在使用Cockpit CMS作为一个新项目的后端,我已经将我的index.php设置为具有背景图像。

现在,我希望用户能够在不删除其他图像的情况下更改"库"中的图像,并且我希望加载"库"的最后一张图像。

这是Cockpit Galleries的文档。

我可以用这个来做:

<?php foreach(gallery('Backgrounds') as $images): ?>
    <?php thumbnail($images["path"]) ?>
<?php endforeach;?>

然而,这加载了所有的图像,而且非常缓慢和低效,我想做的是将图像加载到一个数组中并使用最后一个图像,这就是我目前所拥有的:

<?php $images = cockpit("galleries")->gallery('Backgrounds'); ?>
<?php thumbnail(end($images["path"])) ?>

更新:

已检查PHP日志,我收到此错误:

PHP Warning:  end() expects parameter 1 to be array, null given in /Users/username/Developer/Beardedweb/index.php on line 10

抱歉,PHP不是很熟练。

干杯,奥蒂斯·赖特。

你很接近。首先,您必须获取数组的最后一个元素,然后获取它的值。

<?php 
$img = end($images);
thumbnail($img["path"]);
?>