开发者

php list array by random date

I need to list an array by date, this is what I have so far which spits out the image name and gives it a random date:

$images = glob('images/{*.jpg,*.gif,*.png}', GLOB_BRACE);

foreach($images as $image) 
 {
  $image  = str_replace('images/','',$image);
  $date   = date('Y-m-d', strtotime( '-'.mt_rand(0,1351).' days'));
  $result = $image . '<br />' . $date . '<br /><br />';

echo $result;

}

Example:

    16631824.jpg
    2008-04-23

    17122028.jpg
    2007-12-31

    1854815.jpg
    2007-10-13

    1gaffuv.jpg
    2009-04-12

    3rekel7c.jpg
    2010-06-13

Now I jus开发者_开发知识库t need help listing it by date ...as always any help is appreciated.


$images = glob('images/{*.jpg,*.gif,*.png}', GLOB_BRACE);

$sorted = array();
foreach($images as $image) 
{
  $image  = str_replace('images/','',$image);
  $timestamp   = strtotime('-'.mt_rand(0,1351).' days');
  $sorted[$timestamp] = $image;
}

ksort($sorted);

foreach ( $sorted as $timestamp => $image )
{
  $date = date('Y-m-d', $timestamp);
  $result = $image . '<br />' . $date . '<br /><br />';
  echo $result;
}


Create an array of images, using the date as the key, and then sort the keys before output.

$images_arr = array();
foreach($images as $image)
{
  $date   = date('Y-m-d', strtotime( '-'.mt_rand(0,1351).' days'));
  $images_arr[$date] = str_replace('images/','',$image);
}
// Sort on array keys
ksort($images_arr);

// Output your array however you need to
foreach($images_arr as $img)
{
  // output
}
0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜