dongqing7789 2015-12-01 11:29
浏览 42

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

Hi i've created an array with 4 rows and 5 columns. 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. 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. 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>";
    ?>
  • 写回答

1条回答 默认 最新

  • dongneng5383 2015-12-01 11:44
    关注

    I added PHP's usort function

    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

    Here, your random number is in index 3 and we are comparing numbers in that index

    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
            )
    
    )
    
    评论

报告相同问题?

悬赏问题

  • ¥15 对于squad数据集的基于bert模型的微调
  • ¥15 为什么我运行这个网络会出现以下报错?CRNN神经网络
  • ¥20 steam下载游戏占用内存
  • ¥15 CST保存项目时失败
  • ¥15 树莓派5怎么用camera module 3啊
  • ¥20 java在应用程序里获取不到扬声器设备
  • ¥15 echarts动画效果的问题,请帮我添加一个动画。不要机器人回答。
  • ¥15 Attention is all you need 的代码运行
  • ¥15 一个服务器已经有一个系统了如果用usb再装一个系统,原来的系统会被覆盖掉吗
  • ¥15 使用esm_msa1_t12_100M_UR50S蛋白质语言模型进行零样本预测时,终端显示出了sequence handled的进度条,但是并不出结果就自动终止回到命令提示行了是怎么回事: