【问题标题】:PHP: Sorting array with natsortPHP:使用 natsort 对数组进行排序
【发布时间】:2009-07-27 03:18:33
【问题描述】:

我有一个 PHP 脚本,它可以创建一个缩略图并列出一个图片库。我遇到的问题是它按服务器上的时间戳列出它,但我希望它“自然地”列出。

<?php
# SETTINGS
$max_width = 100;
$max_height = 100;
$per_page = 24;

$page = $_GET['page'];

$has_previous = false;
$has_next = false;

function getPictures() {
    global $page, $per_page, $has_previous, $has_next;
    if ( $handle = opendir(".") ) {
        $lightbox = rand();
        echo '<ul id="pictures">';

        $count = 0;
        $skip = $page * $per_page;

        if ( $skip != 0 )
            $has_previous = true;

        while ( $count < $skip && ($file = readdir($handle)) !== false ) {
            if ( !is_dir($file) && ($type = getPictureType($file)) != '' )
                $count++;
        }
        $count = 0;
        while ( $count < $per_page && ($file = readdir($handle)) !== false ) {
            if ( !is_dir($file) && ($type = getPictureType($file)) != '' ) {
                if ( ! is_dir('thumbs') ) {
                    mkdir('thumbs');
                }
                if ( ! file_exists('thumbs/'.$file) ) {
                    makeThumb( $file, $type );
                }
                echo '<li><a href="'.$file.'" class="zoom" rel="group">';
                echo '<img src="thumbs/'.$file.'" alt="" />';
                echo '</a></li>';
                $count++;
            }
        }
        echo '</ul>';

        while ( ($file = readdir($handle)) !== false ) {
            if ( !is_dir($file) && ($type = getPictureType($file)) != '' ) {
                $has_next = true;
                break;
            }
        }
    }
}

function getPictureType($file) {
    $split = explode('.', $file); 
    $ext = $split[count($split) - 1];
    if ( preg_match('/jpg|jpeg/i', $ext) ) {
        return 'jpg';
    } else if ( preg_match('/png/i', $ext) ) {
        return 'png';
    } else if ( preg_match('/gif/i', $ext) ) {
        return 'gif';
    } else {
        return '';
    }
}

function makeThumb( $file, $type ) {
    global $max_width, $max_height;
    if ( $type == 'jpg' ) {
        $src = imagecreatefromjpeg($file);
    } else if ( $type == 'png' ) {
        $src = imagecreatefrompng($file);
    } else if ( $type == 'gif' ) {
        $src = imagecreatefromgif($file);
    }
    if ( ($oldW = imagesx($src)) < ($oldH = imagesy($src)) ) {
        $newW = $oldW * ($max_width / $oldH);
        $newH = $max_height;
    } else {
        $newW = $max_width;
        $newH = $oldH * ($max_height / $oldW);
    }
    $new = imagecreatetruecolor($newW, $newH);
    imagecopyresampled($new, $src, 0, 0, 0, 0, $newW, $newH, $oldW, $oldH);
    if ( $type == 'jpg' ) {
        imagejpeg($new, 'thumbs/'.$file);
    } else if ( $type == 'png' ) {
        imagepng($new, 'thumbs/'.$file);
    } else if ( $type == 'gif' ) {
        imagegif($new, 'thumbs/'.$file);
    }
    imagedestroy($new);
    imagedestroy($src);
}
?>

【问题讨论】:

  • “自然”排序到底是什么意思?
  • @thedz:就像人类一样。 php.net/natsort
  • @hobodave:啊,有趣。不知道。谢谢!

标签: php arrays sorting


【解决方案1】:

与 hobodave 类似,但我会使用 php 内置的 natsort 函数:


uksort($output, "strnatcmp");

【讨论】:

    【解决方案2】:

    你甚至没有使用数组。

    当你遇到它们时,你需要将它们放入一个数组中,而不是回显你的 li,由文件名索引。

    $output[$file] = '<li>etc</li>';
    

    然后,一旦您的循环完成,您将需要使用自定义函数进行自然键排序,因为 PHP 的 natsort() 仅适用于值。

    function natksort($array) {
        $keys = array_keys($array);
        natsort($keys);
    
        $ret = array();
        foreach ($keys as $k) {
            $ret[$k] = $array[$k];
        }
    
        return $ret;
    }
    
    $output = natksort($output);
    echo '<ul>';
    foreach ($output as $out) {
        echo $out;
    }
    echo '</ul>';
    

    编辑

    哇,我找到了这个小宝石来进行排序:

    uksort($array, 'strnatcasecmp');
    

    信用:http://www.chipstips.com/?p=269

    【讨论】:

    • 该死的我也发现了。我对 SO 太慢了 :(
    【解决方案3】:

    诀窍是将所有内容放在一个数组中...我冒昧地重写了您的 getPictures() 函数...这个实现了排序。

    function getPictures() {
        global $page, $per_page, $has_previous, $has_next;
    
        if (!is_dir('thumbs')) {
            mkdir('thumbs');
        }
    
        if ($handle = opendir(".")) {
            $lightbox = rand();
    
            $files = array();
    
            while (($file = readdir($handle)) !== false) {
                if (!is_dir($file) && ($type = getPictureType($file)) != '') {
                    $files[] = $file;
                }
            }
    
            natsort($files);
    
            $has_previous = $skip != 0;
            $has_next = (count($files) - $skip - $per_page) > 0;
    
            $spliceLength = min($per_page, count($files) - $skip);
            $files = array_slice($files, $skip, $spliceLength);
    
            echo '<ul id="pictures">';
    
            foreach($files as $file) {
                if (!file_exists('thumbs/' . $file)) {
                    $type = getPictureType($file);
                    makeThumb($file, $type);
                }
    
                echo '<li><a href="' . $file . '" class="zoom" rel="group">';
    
                echo '<img src="thumbs/' . $file . '" alt="" />';
    
                echo '</a></li>';
            }
    
            echo '</ul>';
        }
    }
    

    【讨论】:

    • 猜我只是个菜鸟。我将它插入到 getpictures 中,但现在什么都没有显示。
    • 甜蜜。谢谢你牵着我的手。欣赏它!
    • Andrew - natsort 目前正在工作,但是当我点击下一页时,它将显示下一页但重新加载相同的图像
    • 没关系,我添加了这个... $count = 0; $skip = $page * $per_page; if ($skip != 0) $has_previous = true;谢谢!
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2010-10-26
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2011-02-02
    • 1970-01-01
    相关资源
    最近更新 更多