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 cad图纸,chx-3六轴码垛机器人
  • ¥15 移动摄像头专网需要解vlan
  • ¥20 access多表提取相同字段数据并合并
  • ¥20 基于MSP430f5529的MPU6050驱动,求出欧拉角
  • ¥20 Java-Oj-桌布的计算
  • ¥15 powerbuilder中的datawindow数据整合到新的DataWindow
  • ¥20 有人知道这种图怎么画吗?
  • ¥15 pyqt6如何引用qrc文件加载里面的的资源
  • ¥15 安卓JNI项目使用lua上的问题
  • ¥20 RL+GNN解决人员排班问题时梯度消失