PHP数组-按数组外计算的值排序(从高到低)

PHP数组-按数组外计算的值排序(从高到低),php,arrays,sorting,directory,Php,Arrays,Sorting,Directory,问题: <?php //make all items in the array unique $dir_list = array_unique($dir_list); //create new array to sort into $dir_list_sort = array(); //for each item in the array foreach($dir_list as $dir) { //find depth of array $dir_depth = subs

问题:

<?php
//make all items in the array unique
$dir_list = array_unique($dir_list);
//create new array to sort into
$dir_list_sort = array();
//for each item in the array
foreach($dir_list as $dir)
{
    //find depth of array
    $dir_depth = substr_count($dir , DIRECTORY_SEPARATOR);
    //stuff that is written to the page separated by a #
    echo $dir_depth." # ".$dir."<br>";
}
?>
我需要将数组(项目符号列表中显示的内容按照它们在数组中出现的顺序)按左侧数字的顺序(从高到低)排序

这些数字对应于右侧目录路径中的分区数(它们当前未存储在阵列中…)

我的问题出现了,因为我不知道如何按照示例中给出的值对数组进行排序,因为它们在数组之外。我尝试过使用多维数组,但是这只会导致更多的混乱

由于下面列出的代码而在屏幕上输出:

<?php
//make all items in the array unique
$dir_list = array_unique($dir_list);
//create new array to sort into
$dir_list_sort = array();
//for each item in the array
foreach($dir_list as $dir)
{
    //find depth of array
    $dir_depth = substr_count($dir , DIRECTORY_SEPARATOR);
    //stuff that is written to the page separated by a #
    echo $dir_depth." # ".$dir."<br>";
}
?>
  • 6#C:\Program Files(x86)\wamp\www\planner\import\homeworktasks
  • 5#C:\Program Files(x86)\wamp\www\planner\import
  • 7#C:\Program Files(x86)\wamp\www\planner\import\homeworktasks\11
  • 7#C:\Program Files(x86)\wamp\www\planner\import\homeworktasks\15
  • 7#C:\Program Files(x86)\wamp\www\planner\import\homeworktasks\17
  • 7#C:\Program Files(x86)\wamp\www\planner\import\homeworktasks\9
  • 7#C:\Program Files(x86)\wamp\www\planner\import\homeworktasks\test
  • 8 C:\ProgramFiles(x86)\wamp\www\planner\import\homeworktasks\test\inside
代码:

<?php
//make all items in the array unique
$dir_list = array_unique($dir_list);
//create new array to sort into
$dir_list_sort = array();
//for each item in the array
foreach($dir_list as $dir)
{
    //find depth of array
    $dir_depth = substr_count($dir , DIRECTORY_SEPARATOR);
    //stuff that is written to the page separated by a #
    echo $dir_depth." # ".$dir."<br>";
}
?>

这不需要多维数组。正常的
usort
就可以了

usort($dir_list, 'compareDirectoryDepth');

function compareDirectoryDepth($dir1, $dir2) {
    $c1 = substr_count($dir1 , DIRECTORY_SEPARATOR);
    $c2 = substr_count($dir2 , DIRECTORY_SEPARATOR);

    return ($c1 == $c2 ? 0 : ($c1 < $c2 ? -1 : 1));
}
usort($dir_list,'compareDirectoryDepth');
函数compareDirectoryDepth($dir1,$dir2){
$c1=子目录计数($dir1,目录分隔符);
$c2=子目录计数($dir2,目录分隔符);
回报($c1==$c2?0:($c1<$c2?-1:1));
}

当然,这可以优化一点,所以子数组计数被称为稍微少一点

您不需要多维数组。正常的
usort
就可以了

usort($dir_list, 'compareDirectoryDepth');

function compareDirectoryDepth($dir1, $dir2) {
    $c1 = substr_count($dir1 , DIRECTORY_SEPARATOR);
    $c2 = substr_count($dir2 , DIRECTORY_SEPARATOR);

    return ($c1 == $c2 ? 0 : ($c1 < $c2 ? -1 : 1));
}
usort($dir_list,'compareDirectoryDepth');
函数compareDirectoryDepth($dir1,$dir2){
$c1=子目录计数($dir1,目录分隔符);
$c2=子目录计数($dir2,目录分隔符);
回报($c1==$c2?0:($c1<$c2?-1:1));
}
当然,这可以稍微优化一下,因此substr_count被称为稍微少一点

您可以使用PHP的函数
usort()
“将使用用户提供的比较函数按值对数组进行排序。”

您必须编写一个可以比较两个值并返回-1、0或1的函数

<?php

// This is just a shortcut for determining the directory depth
function dir_depth($directory_name)
{
    return substr_count($directory_name, DIRECTORY_SEPARATOR);
}

// Takes two values ($a and $b) and returns either -1, 0 or 1
function compare($a, $b)
{
    $depth_a = dir_depth($a);
    $depth_b = dir_depth($b));

    if ($depth_a == $depth_b) {
        // If they have the same depth, return 0
        return 0;
    }

    // If depth_a is smaller than depth_b, return -1; otherwise return 1
    return ($depth_a < $depth_b) ? -1 : 1;
}

// Now we can sort the array.
// usort() needs two parameters:
// 1. the array that will be reordered
// 2. the name of the function that compares two values
usort($dir_list, 'compare');

// Now display the list
foreach ($dir_list as $dir) {
    // here we can use our dir_depth() function again
    echo dir_depth($dir) . ' # ' . $dir . '<br>';
}
您可以使用PHP的函数
usort()
“将使用用户提供的比较函数按值对数组进行排序。”

您必须编写一个可以比较两个值并返回-1、0或1的函数

<?php

// This is just a shortcut for determining the directory depth
function dir_depth($directory_name)
{
    return substr_count($directory_name, DIRECTORY_SEPARATOR);
}

// Takes two values ($a and $b) and returns either -1, 0 or 1
function compare($a, $b)
{
    $depth_a = dir_depth($a);
    $depth_b = dir_depth($b));

    if ($depth_a == $depth_b) {
        // If they have the same depth, return 0
        return 0;
    }

    // If depth_a is smaller than depth_b, return -1; otherwise return 1
    return ($depth_a < $depth_b) ? -1 : 1;
}

// Now we can sort the array.
// usort() needs two parameters:
// 1. the array that will be reordered
// 2. the name of the function that compares two values
usort($dir_list, 'compare');

// Now display the list
foreach ($dir_list as $dir) {
    // here we can use our dir_depth() function again
    echo dir_depth($dir) . ' # ' . $dir . '<br>';
}

将这两个值都放入数组(您称之为“多维”)确实是最简单的解决方案–然后您只需要一个小小的自行编写的比较函数,就可以与
usort
一起使用了。通过创建数组的方式,数组的管理可能会得到改进。您可以显示生成数组的代码吗?作为多维数组的替代方案,带有两个参数(计数数组和原始数组)的
array\u multisort
在这里也可以正常工作。这是一行。将这两个值放入数组中,就像你所说的“多维”将是最简单的解决方案——然后你只需要一个小小的自编比较函数,你就可以使用
usort
,就完成了。通过创建数组的方式,数组的管理可能会得到改进。您可以显示生成数组的代码吗?作为多维数组的替代方案,带有两个参数(计数数组和原始数组)的
array\u multisort
在这里也可以正常工作。这是一条线。