duanbeng6709 2014-07-13 21:57
浏览 50
已采纳

PHP按键排序第二个数组ASC

I have an array inside an array that has the filename, modification time and size, but I need to be able to order the array either ascending or descending by each one of these properties.

I have the following, which gets the information

//SCAN THE DIRECTORY
$directories = scandir($dir);
$directinfo = array();
foreach($directories as $directory){
    if ($directory === '.' or $directory === '..') continue;
    if(!stat($dir.'/'.$directory)){

    } else {
        $filestat = stat($dir.'/'.$directory);
        $directinfo[] = array(
            'name' => $directory,
            'modtime' => $filestat['mtime'],
            'size' => $filestat['size']
        );
    }
}

The array is structured as so:

Array ( 
    [0] => Array ( 
        [name] => 0 Organisation Details 
        [modtime] => 1398164749 
        [size] => 4096
    ) 
    [1] => Array ( 
        [name] => 1 Permission Form 
        [modtime] => 1398164749 
        [size] => 4096 
    ) 
    [2] => Array ( 
        [name] => 6 Invoices 
        [modtime] => 1400802471 
        [size] => 4096 
    ) 
)

and then use this to output:

foreach($directinfo as $dirInfo){
    foreach($dirInfo as $key=>$drInfo){
        echo "Output: ".$key."=>".$drInfo."<br />";
    }
}

But I need to arrange the array before this, and somehow make it so I don't need two arrays or I'm suspecting the ordered output wouldn't work.

I've looked at array_multisort but can't figure out how this would work in this instance.

Any help with this is really appreciated.

  • 写回答

2条回答 默认 最新

  • drk49438 2014-07-13 22:39
    关注

    Using example #3 on the array_multisort manual page:

    $directories = array( 
    array('name'=>'filec','modtime'=>'12303403434','size'=>'12401'),
    array('name'=>'fileb','modtime'=>'12303403432','size'=>'12400'), 
    array('name'=>'filez', 'modtime'=>'12304445405','size'=>'65200')
    );
    
    // Obtain a list of columns
    $name = $modtime = $size = array();
    foreach ($directories as $key => $row) {
        $name[$key]  = $row['name'];
        $modtime[$key] = $row['modtime'];
        $size[$key] = $row['size'];
    }
    
    // Sort the data with name ascending, modtime ascending, size ascending
    array_multisort($name, SORT_ASC, $modtime, SORT_ASC, $size, SORT_ASC, $directories);
    
    print_r($directories);
    

    Online demo here

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

报告相同问题?

悬赏问题

  • ¥15 公交车和无人机协同运输
  • ¥15 stm32代码移植没反应
  • ¥15 matlab基于pde算法图像修复,为什么只能对示例图像有效
  • ¥100 连续两帧图像高速减法
  • ¥15 组策略中的计算机配置策略无法下发
  • ¥15 如何绘制动力学系统的相图
  • ¥15 对接wps接口实现获取元数据
  • ¥20 给自己本科IT专业毕业的妹m找个实习工作
  • ¥15 用友U8:向一个无法连接的网络尝试了一个套接字操作,如何解决?
  • ¥30 我的代码按理说完成了模型的搭建、训练、验证测试等工作(标签-网络|关键词-变化检测)