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

时间:2015-12-01 11:29:58

标签: php arrays sorting multidimensional-array

您好我创建了一个包含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>";
    ?>

1 个答案:

答案 0 :(得分:0)

我添加了PHP的usort函数

myMap.MapElementClick += MyMap_MapElementClick;
private void MyMap_MapElementClick(MapControl sender, MapElementClickEventArgs args)
        {
          // add your code to position the canvas (pop up) and display it here
        }
  

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

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

所以你的代码是:

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

usort($times_table, 'sortByRandomNo');

和你的输出:

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