dongzhui9936
2015-12-12 18:28 阅读 51
已采纳

在php中,如何根据特定的(在本例中为最后一个)路径片段对路径数组进行排序?

I have an array like so:

[0] => /home/user/public_html/things/1 - First thing/
[1] => /home/user/public_html/things/3 - Third thing/
[2] => /home/user/public_html/things/2 - Second thing/

What's the straightforward way to sort this array so the 'things' go 1, 2, 3?

I've used natsort() on just the last path-fragment but it's not proving straightforward to use... I chopped the rest of the path off, sorted, then put it back on but that's ugly and it can change in the mean time so I'm hoping for a better way than that.

Any and all suggestions would be greatly appreciated, I'm sure there's several ways to approach this :)

  • 点赞
  • 写回答
  • 关注问题
  • 收藏
  • 复制链接分享

4条回答 默认 最新

  • 已采纳
    dongza3124 dongza3124 2015-12-12 19:43

    Taking your question as it stands (sort an array of paths based on a specific path-fragment), I thought of a function that would allow you to specify the fragment you want the paths to be sorted on.

    As the paths in the example only differ in their last part, the above is not very interesting when applied on that example. So I will use a slightly different list:

    $paths = [
        '/home/user/public_html/things/sub/1',
        '/home/user/public_html/things/3',
        '/home/admin/public_html/things/2'
    ];
    
    function build_sorter($part) {
        return function ($a, $b) use ($part) {
            return strnatcmp(
                implode("/", array_slice(explode("/", $a), $part)),
                implode("/", array_slice(explode("/", $b), $part))
            );
        };
    }
    
    // sort by second part in paths:
    usort($paths, build_sorter(2));
    print_r ($paths);
    

    The output generated has put the "admin" folder first:

       Array (
         [0] => /home/admin/public_html/things/2
         [1] => /home/user/public_html/things/3
         [2] => /home/user/public_html/things/sub/1
       ) 
    

    Sorting by the last part would go like this:

    usort($paths, build_sorter(-1));
    print_r ($paths);
    

    Output:

       Array (
         [0] => /home/user/public_html/things/sub/1
         [1] => /home/admin/public_html/things/2
         [2] => /home/user/public_html/things/3
       ) 
    

    So the argument you pass to builder_sort is the offset (positive from left, negative from right) of the part of the paths you want to base your sort on.

    点赞 评论 复制链接分享
  • dqsong2010 dqsong2010 2015-12-12 18:34

    Applying asort function can do it.

    $p = [
        '/home/user/public_html/things/1',
        '/home/user/public_html/things/3',
        '/home/user/public_html/things/2'
    ];
    
    asort($p);
    
    print_r($p);
    
    点赞 评论 复制链接分享
  • dsfdsf48652 dsfdsf48652 2015-12-12 19:09

    I'm this approach:

    1. find the 1st integer in the path
    2. define the found int value as the key for the current path in a new array
    3. sort the new array with ksort()
    
        $paths = [
            '/home/user/public_html/things/1 - First thing/',
            '/home/user/public_html/things/3 - Third thing/',
            '/home/user/public_html/things/2 - Second thing/'
        ];
    
        $matches = [];
        $sorted_paths = [];
    
        foreach ($paths as $path) {
            if (preg_match('/(\d+)/', $path, $matches)) {
                $sorted_paths[$matches[1]] = $path;
            }
        }
    
        ksort($sorted_paths);
        print_r($sorted_paths);
    

    As the result, I've got something like this:

    Array
    (
        [1] => /home/user/public_html/things/1 - First thing/
        [2] => /home/user/public_html/things/2 - Second thing/
        [3] => /home/user/public_html/things/3 - Third thing/
    )
    
    点赞 评论 复制链接分享
  • dqk42179 dqk42179 2015-12-12 19:35

    From what i comprehend you want to sort your array results by the number after the thingy folder.

    <?php 
    $Directories = array('/home/user/public_html/things/1', '/home/user/public_html/things/3', '/home/user/public_html/things/2');
    
    $SortedNums = array();
    
    foreach ( $Directories as $Dir )
    {
        $Nums = explode("/", $Dir);
    
        foreach ( $Nums as $Num )
        {
            if ( is_numeric($Num) )
            {
                array_push($SortedNums, $Num);
            }   
        }
    }
    
    sort($SortedNums);
    
    $ArrayLength=count($SortedNums);
    for($x=0;$x<$ArrayLength;$x++)
    {
       echo $SortedNums[$x];
       echo "<br>";
    }
    ?>
    

    Produces the following output:

    1 2 3

    点赞 评论 复制链接分享

相关推荐