用最新的图片库对PHP图片库进行排序
我使用一个php代码来显示网站上一个产品文件夹中的产品列表 无论何时添加新图片,都应显示第一张 下面是我在页面上使用的代码用最新的图片库对PHP图片库进行排序,php,image,sorting,gallery,Php,Image,Sorting,Gallery,我使用一个php代码来显示网站上一个产品文件夹中的产品列表 无论何时添加新图片,都应显示第一张 下面是我在页面上使用的代码 <div id="galleryListWrapper"> <?php if (!empty($galleryArray) && $galleryArray['stats']['total_images'] > 0): ?> <ul id="galleryList" class="clearfix"
<div id="galleryListWrapper">
<?php if (!empty($galleryArray) && $galleryArray['stats']['total_images'] > 0): ?>
<ul id="galleryList" class="clearfix">
<?php foreach ($galleryArray['images'] as $image): ?>
<li><a href="<?php echo html_entity_decode($image['file_path']); ?>" title="<?php echo $image['file_title']; ?>" rel="colorbox"><img src="<?php echo $image['thumb_path']; ?>" alt="<?php echo $image['file_title']; ?>"/></a></li>
<?php endforeach; ?>
</ul>
<?php endif; ?>
</div>
从目录获取文件时,在文件数组中添加文件创建日期的键。为了进行排序,只需使用filemtime($filepath)就足够了
要按时间戳对数组进行排序,请查看
出于您的目的,它看起来像这样:
function sortByTimestamp( $a, $b ) {
if ($a[ 'timestamp' ] == $b[ 'timestamp' ]) {
return 0;
}
return ($a[ 'timestamp' ] > $b[ 'timestamp' ]) ? -1 : 1;
}
usort( $galleryArray[ 'images' ], 'sortByTimestamp' );
存储上载日期或使用图像文件的创建日期在输出前对数组进行排序。
function sortByTimestamp( $a, $b ) {
if ($a[ 'timestamp' ] == $b[ 'timestamp' ]) {
return 0;
}
return ($a[ 'timestamp' ] > $b[ 'timestamp' ]) ? -1 : 1;
}
usort( $galleryArray[ 'images' ], 'sortByTimestamp' );