【问题标题】:How to sort a multidimensional array by randNum如何通过 randNum 对多维数组进行排序
【发布时间】:2015-12-01 11:29:57
【问题描述】:

您好,我创建了一个 4 行 5 列的数组。我现在想按数组中的随机数对数组进行排序,而不是如何对多维数组进行排序。我在网上看到我可能对每个循环都使用了一个,但如果我要使用一个,我不确定在哪里放置它。另外,我不确定如何告诉 sortarray 我要对哪一列进行排序,因为我没有任何输出 id。任何帮助将不胜感激。

<?php 
$vyear = 1;
$vmonth= 3;
$date = "2015-11-25";
$t = 0;

echo date("M-y") . "<br>";

$startdate = "2009/06/01";

$start = strtotime($date);

$currentdate = $start;

$newdate = strtotime (  $t .'month' , strtotime ( $date ) ) ;
$ndate = date ( 'm-Y-d' , $newdate );

echo $ndate;



 echo "<br>";
 echo "<br>";
 echo $date;

$times_table = array();
        for($i = 0; $i <= 3; $i++){
            $times_table[$i] = array();

        }
echo "<pre>";

       for($i = 0; $i <= 3; $i++){
             for($j = 0; $j <= 4; $j++){

               if ($j == 0){
                $times_table[$i][$j]=  "Version 4" ;
            }
                else if ($j == 1){
                $cur_date = date("M-y", $currentdate);

                $currentdate = strtotime('+1 month', $currentdate);

                $times_table[$i][$j]= $cur_date ;

          echo  $cur_date . ">". "<br />";
                }
                else{
                    $times_table[$i][$j]=  "gary" ;
                }
                if ($j == 3) {
                    $numbers = mt_rand(1, 100);
                    $times_table[$i][$j]= $numbers ;

                }
                if ($j == 4){

                    if($i == 0 || $i == 3)
                    {
                        $pay = "P";

                    $times_table[$i][$j]= $pay ;
                    }
                    else{
                        $int = "I";

                    $times_table[$i][$j]= $int ;

                    }
                }

            }

            }




// echo $times_table[1][3] ;
print_r($times_table);
 echo "</pre>";
    ?>

【问题讨论】:

    标签: php arrays sorting multidimensional-array


    【解决方案1】:

    我添加了PHP的usort函数

    function sortByRandomNo($a, $b) {
        return $a[3] - $b[3];
    }
    
    usort($times_table, 'sortByRandomNo');
    

    usort - 使用用户定义的比较函数按值对数组进行排序

    这里,您的随机数在索引 3 中,我们正在比较该索引中的数字

    所以你的代码是:

    <?php 
    $vyear = 1;
    $vmonth= 3;
    $date = "2015-11-25";
    $t = 0;
    
    echo date("M-y") . "<br>";
    
    $startdate = "2009/06/01";
    
    $start = strtotime($date);
    
    $currentdate = $start;
    
    $newdate = strtotime (  $t .'month' , strtotime ( $date ) ) ;
    $ndate = date ( 'm-Y-d' , $newdate );
    
    echo $ndate;
    
    
    
     echo "<br>";
     echo "<br>";
     echo $date;
    
    $times_table = array();
            for($i = 0; $i <= 3; $i++){
                $times_table[$i] = array();
    
            }
    echo "<pre>";
    
           for($i = 0; $i <= 3; $i++){
                 for($j = 0; $j <= 4; $j++){
    
                   if ($j == 0){
                    $times_table[$i][$j]=  "Version 4" ;
                }
                    else if ($j == 1){
                    $cur_date = date("M-y", $currentdate);
    
                    $currentdate = strtotime('+1 month', $currentdate);
    
                    $times_table[$i][$j]= $cur_date ;
    
              echo  $cur_date . ">". "<br />";
                    }
                    else{
                        $times_table[$i][$j]=  "gary" ;
                    }
                    if ($j == 3) {
                        $numbers = mt_rand(1, 100);
                        $times_table[$i][$j]= $numbers ;
    
                    }
                    if ($j == 4){
    
                        if($i == 0 || $i == 3)
                        {
                            $pay = "P";
    
                        $times_table[$i][$j]= $pay ;
                        }
                        else{
                            $int = "I";
    
                        $times_table[$i][$j]= $int ;
    
                        }
                    }
    
                }
    
                }
    
    
    // echo $times_table[1][3] ;
    print_r($times_table);
     echo "</pre>";
    
    function sortByRandomNo($a, $b) {
        return $a[3] - $b[3];
    }
    
    usort($times_table, 'sortByRandomNo');
    
    echo "<pre>";
    print_r($times_table);
    echo "</pre>";
    

    你的输出:

    Dec-15
    11-2015-25
    
    2015-11-25
    Nov-15>
    Dec-15>
    Jan-16>
    Feb-16>
    Array
    (
        [0] => Array
            (
                [0] => Version 4
                [1] => Nov-15
                [2] => gary
                [3] => 2
                [4] => P
            )
    
        [1] => Array
            (
                [0] => Version 4
                [1] => Dec-15
                [2] => gary
                [3] => 9
                [4] => I
            )
    
        [2] => Array
            (
                [0] => Version 4
                [1] => Jan-16
                [2] => gary
                [3] => 43
                [4] => I
            )
    
        [3] => Array
            (
                [0] => Version 4
                [1] => Feb-16
                [2] => gary
                [3] => 45
                [4] => P
            )
    
    )
    Array
    (
        [0] => Array
            (
                [0] => Version 4
                [1] => Nov-15
                [2] => gary
                [3] => 2
                [4] => P
            )
    
        [1] => Array
            (
                [0] => Version 4
                [1] => Dec-15
                [2] => gary
                [3] => 9
                [4] => I
            )
    
        [2] => Array
            (
                [0] => Version 4
                [1] => Jan-16
                [2] => gary
                [3] => 43
                [4] => I
            )
    
        [3] => Array
            (
                [0] => Version 4
                [1] => Feb-16
                [2] => gary
                [3] => 45
                [4] => P
            )
    
    )
    

    【讨论】:

    • 感谢您的回复。对不起,我能问一下 usort 功能是如何工作的。刚接触这个,还没有像这样排序。
    • 查看更新后的答案! usort - 使用用户定义的比较函数按值对数组进行排序 - php.net/manual/en/function.usort.php
    • 你会用同样的方式写arsort吗?
    • 您不能为arsort添加用户定义的回调,而是将return $a[3] - $b[3];替换为return $b[3] - $a[3];以获得相反的结果
    猜你喜欢
    • 2012-11-15
    • 2012-06-10
    • 1970-01-01
    • 2021-06-29
    • 2011-02-02
    • 1970-01-01
    • 2021-12-27
    • 2017-07-03
    相关资源
    最近更新 更多