PHP 对数组[22,27,16,32,12,45,99,77,66]进行排序,输出结果为:[99 12 77 16 66 22 45 27 32],需要用for循环

解决方案 »

  1.   

    用PHP中for循环遍历数组,使数组[22,27,16,32,12,45,99,77,66]进行排序最后的输出结果是数组[99 12 77 16 66 22 45 27 32]
      

  2.   


                    $data=[22,27,16,32,12,45,99,77,66];
    rsort($data);
    $group=ceil(count($data)/2);
    $fristData=array_slice($data,0,$group);
    $seData=array_slice($data,$group);
    sort($seData);
    $newData=[];
    for($i=0;$i<count($fristData);$i++){
    array_push($newData,$fristData[$i]);
    if(isset($seData[$i])){
    array_push($newData,$seData[$i]);
    }
    }
    print_r($newData);
    exit;结果和你的一样,不知道是不是你想要的?
      

  3.   

    先把数组排序 一分为二 然后对执行array_slice的数组重新排序  然后循环加入新数组这思维 绝对。