duanqiao2006 2010-12-28 05:36
浏览 27
已采纳

新闻系统的数组排序问题

I'm currently stuck trying to figure out how to sort my array files. I have a simple news posting system that stores the content in seperate .dat files and then stores them in an array. I numbered the files so that my array can sort them from lowest number to greatest; however, I have run into a small problem. To begin here is some more information on my system so that you can understand it better.

The function that gathers my files is:

function getNewsList() {
        $fileList = array();
        // Open the actual directory
        if($handle = opendir(ABSPATH . ADMIN . "data")) {
            // Read all file from the actual directory
            while($file = readdir($handle))  {
                if(!is_dir($file)) {
                    $fileList[] = $file;
                }
            }
        }
        // Return the array.
        return $fileList;
    }

On a seperate file is the programming that processes the news post. I didn't post that code for simplicity's sake but I will explain how the files are named. The files are numbered and the part of the post's title is used... for the numbering I get a count of the array and add "1" as an offset. I get the title of the post, encode it to make it file-name-friendly and limit the amount of text so by the end of it all I end up with:

// Make the variable that names the file that will contain
  // the post.
  $filename = "00{$newnumrows}_{$snipEncode}";

When running print_r on the above function I get:

Array (
     [0] => 0010_Mira_mi_Soledad.dat
     [1] => 0011_WOah.dat
     [2] => 0012_Sinep.dat
     [3] => 0013_Living_in_Warfa.dat
     [4] => 0014_Hello.dat
     [5] => 001_AS.dat
     [6] => 002_ASASA.dat
     [7] => 003_SSASAS.dat
     ...
     [13] => 009_ASADADASADAFDAF.dat
)

And this is how my content is displayed. For some reason according to the array sorting 0010 comes before 001...? Is there a way I can get my array to sort 001 before 0010?

  • 写回答

3条回答 默认 最新

  • dongyili5843 2010-12-28 06:47
    关注

    Take the filename and extract the prefix number as integer number:

    // $filename has the format: "00{$newnumrows}_{$snipEncode}"
    function generateSortKey($filename)
    {
        $separatorPos = stripos($filename, '_');
    
        $prefix = trim(substr($filename, 0, $separatorPos));
        return intval($prefix);
    }
    

    Than create an associative array from the list of files, the keys will be used as sortable value later:

    function toSortableArray($files)
    {
        $result = array();
    
        foreach ($files as $filename)
        {
            $key = generateSortKey($filename);
            $value = $filename;
    
            $result[$key] = $value;
        }
    
        return $result;
    }
    

    and at last use krsort():

    $list = getNewsList();
    $sortableList = toSortableArray($list);
    
    krsort($sortableList);   // after that $sortableList is
                             // sorted by key in descending order now
    

    FIX: ksort() => krsort()

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论
查看更多回答(2条)

报告相同问题?

悬赏问题

  • ¥15 无线电能传输系统MATLAB仿真问题
  • ¥50 如何用脚本实现输入法的热键设置
  • ¥20 我想使用一些网络协议或者部分协议也行,主要想实现类似于traceroute的一定步长内的路由拓扑功能
  • ¥30 深度学习,前后端连接
  • ¥15 孟德尔随机化结果不一致
  • ¥15 apm2.8飞控罗盘bad health,加速度计校准失败
  • ¥15 求解O-S方程的特征值问题给出边界层布拉休斯平行流的中性曲线
  • ¥15 谁有desed数据集呀
  • ¥20 手写数字识别运行c仿真时,程序报错错误代码sim211-100
  • ¥15 关于#hadoop#的问题