简体   繁体   English

如何通过randNum对多维数组排序

[英]How to sort a multidimensional array by randNum

Hi i've created an array with 4 rows and 5 columns. 嗨,我创建了一个具有4行5列的数组。 I now want to sort the array by the random number in my array but not how to sort multidimensional arrays. 现在,我想通过数组中的随机数对数组进行排序,而不是如何对多维数组进行排序。 I seen online that I may have use a for each loop but not sure where to place it if I were to use one. 我在网上看到我可能在每个循环中都使用a,但是不确定如果要使用它时将其放置在何处。 Also I'm not sure how I to tell the sortarray what column I want to sort as I don't have any id for the outputs. 另外,我不确定如何告诉sortarray我要对哪一列进行排序,因为我的输出没有任何ID。 Any help would be myuch appreciated. 任何帮助将不胜感激。

<?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>";
    ?>

I added PHP's usort function 我添加了PHP的usort函数

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

usort($times_table, 'sortByRandomNo');

usort - Sorts an array by values using a user-defined comparison function usort-使用用户定义的比较函数按值对数组进行排序

Here, your random number is in index 3 and we are comparing numbers in that index 在这里,您的随机数在索引3中,我们正在比较该索引中的数字

So your code is: 所以你的代码是:

<?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>";

and your output: 和您的输出:

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
        )

)

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM