duancan9815 2014-11-13 01:37
浏览 32
已采纳

使用PHP数组中的最后一项

Hello I am using Cockpit CMS as the backend for a new project, and I have set my index.php to have a background image.

Now I want the user to be able to change the images in a "gallery" without deleting the other images and I want to load up the last image in the "gallery".

Here is the Cockpit Galleries documentation.

I can do this with this:

<?php foreach(gallery('Backgrounds') as $images): ?>

    <?php thumbnail($images["path"]) ?>

<?php endforeach;?>

however this loads all the images and is really slow and inefficient, what I want to do is load up the images into an array and use the last image, this is what I have so far:

<?php $images = cockpit("galleries")->gallery('Backgrounds'); ?>

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

Update:

Checked PHP log am receiving this error:

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

Sorry not very PHP proficient.

Cheers, Otis Wright.

  • 写回答

1条回答 默认 最新

  • douzi2785 2014-11-13 01:39
    关注

    You're close. First you have to get the last element of the array and then get it's value.

    <?php 
    $img = end($images);
    thumbnail($img["path"]);
    ?>
    
    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?

悬赏问题

  • ¥20 matlab计算中误差
  • ¥15 对于相关问题的求解与代码
  • ¥15 ubuntu子系统密码忘记
  • ¥15 信号傅里叶变换在matlab上遇到的小问题请求帮助
  • ¥15 保护模式-系统加载-段寄存器
  • ¥15 电脑桌面设定一个区域禁止鼠标操作
  • ¥15 求NPF226060磁芯的详细资料
  • ¥15 使用R语言marginaleffects包进行边际效应图绘制
  • ¥20 usb设备兼容性问题
  • ¥15 错误(10048): “调用exui内部功能”库命令的参数“参数4”不能接受空数据。怎么解决啊