Warning: file_get_contents(/data/phpspider/zhask/data//catemap/3/arrays/12.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
PHP:使用natsort对数组进行排序_Php_Arrays_Sorting - Fatal编程技术网

PHP:使用natsort对数组进行排序

PHP:使用natsort对数组进行排序,php,arrays,sorting,Php,Arrays,Sorting,我有一个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

我有一个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);
}
?>

您甚至没有使用数组

你需要将它们放入一个数组中,按文件名索引,而不是在遇到它们时回显它们

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

信用证:

您甚至没有使用数组

你需要将它们放入一个数组中,按文件名索引,而不是在遇到它们时回显它们

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

信用证:

诀窍是将所有内容放入一个数组中。。。我冒昧地重写了你的
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(){ 全局$page,$per_page,$has_previous,$has_next; 如果(!is_dir('thumbs')){ mkdir(“拇指”); } 如果($handle=opendir(“.”){ $lightbox=rand(); $files=array(); while(($file=readdir($handle))!==false){ 如果(!is_dir($file)&($type=getPictureType($file))!=“”){ $files[]=$file; } } natsort($文件); $has_previous=$skip!=0; $has_next=(计数($files)-$skip-$per_page)>0; $strippelength=min($per_page,count($files)-$skip); $files=array\u slice($files,$skip,$slice长度); echo'
    ; foreach($files作为$file){ 如果(!file_存在('thumbs/'.$file)){ $type=getPictureType($file); makeThumb($file,$type); } 回音“
  • ”; } 回声“
”; } }
诀窍是将所有内容放入一个数组中。。。我冒昧地重写了你的
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(){ 全局$page,$per_page,$has_previous,$has_next; 如果(!is_dir('thumbs')){ mkdir(“拇指”); } 如果($handle=opendir(“.”){ $lightbox=rand(); $files=array(); while(($file=readdir($handle))!==false){ 如果(!is_dir($file)&($type=getPictureType($file))!=“”){ $files[]=$file; } } natsort($文件); $has_previous=$skip!=0; $has_next=(计数($files)-$skip-$per_page)>0; $strippelength=min($per_page,count($files)-$skip); $files=array\u slice($files,$skip,$slice长度); echo'
    ; foreach($files作为$file){ 如果(!file_存在('thumbs/'.$file)){ $type=getPictureType($file); makeThumb($file,$type); } 回音“
  • ”; } 回声“
”; } }
与hobodave类似,但我会使用php内置的natsort函数:


uksort($output, "strnatcmp");

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


uksort($output, "strnatcmp");

“自然”排序意味着什么?@thedz:就像人类一样@霍博达夫:啊,很有趣。我不知道。谢谢“自然”排序意味着什么?@thedz:就像人类一样@霍博达夫:啊,很有趣。我不知道。谢谢该死,我也找到了。我太慢了,所以:(该死,我也发现了。我太慢了,所以:(我想我只是一个彻头彻尾的傻瓜。我把这个插件插入getpictures,但现在什么也没有显示。太好了。谢谢你牵着我的手看这个。谢谢!Andrew-natsort目前正在工作,但是当我点击下一页时,它会显示下一页,但会重新加载相同的图像。我添加了这个…$count=0;$skip=$page*$每页;如果($skip!=0)$has_previous=true;谢谢!我想我只是一个彻头彻尾的傻瓜。我把这个插件插入getpictures,但现在什么也没有显示。太好了。谢谢你牵着我的手。谢谢!Andrew-natsort目前正在工作,但是当我点击下一页时,它会显示下一页,但会重新加载相同的图片。我补充道:s、 ..$count=0;$skip=$page*$每页;if($skip!=0)$has\u previous=true;谢谢!