Home >Backend Development >PHP Tutorial >Two-dimensional array sorting

Two-dimensional array sorting

巴扎黑
巴扎黑Original
2016-11-22 15:56:55951browse

<?php
function arr_sort($data){
  
  $len = count($data);
  
  for($i=0;$i<$len;$i++){
  
$ilen = count($data[$i]);
for($j=0;$j < $ilen;$j++){
 
 $m = $data[$i][$j];
 $mpos1 = $i;
 $mpos2 = $j;
 if( !isset($data[$i][$j+1]) ){
$ii = $i+1;
$jj = 0;
 }else{
$ii = $i;
$jj = $j+1;
 }
 for($ii; $ii<$len; $ii++){
$iilen = count($data[$ii]);
for($jj; $jj<$iilen; $jj++){
 if($m > $data[$ii][$jj]){
$m = $data[$ii][$jj];
$mpos1 = $ii;
$mpos2 = $jj;
 }
 if($jj == ($iilen-1)){
$jj = 0;
break;
 }
}
 }
 if($i!=$mpos1 || $j!=$mpos2){
$data[$i][$j] = ($data[$i][$j] + $data[$mpos1][$mpos2]);
$data[$mpos1][$mpos2] = ($data[$i][$j] - $data[$mpos1][$mpos2]);
$data[$i][$j] = ($data[$i][$j] - $data[$mpos1][$mpos2]);
 }
 if($j == ($ilen-1)){
$j = 0;
break;
 }
}
  }
  return $data;
}
$data[0] = array(5,8,11,55,77);
$data[1] = array(1,2,3,9);
print_r(arr_sort($data));


Statement:
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
Previous article:php quick sortNext article:php quick sort