PHP ksort
1.例子一:
1 <?php 2 /** 3 * 根据 c1 对元素排序 4 */ 5 $arrays = [ 6 'b' => [ 7 'c1' => 10, 8 'c2' => 5, 9 ], 10 'a' => [ 11 'c1' => 5, 12 'c2' => 8, 13 ], 14 'c' => [ 15 'c1' => 9, 16 'c2' => 10, 17 ], 18 'e' => [ 19 'c1' => 5, 20 'c2' => 10, 21 ], 22 ]; 23 24 $t = []; 25 $c = 'c1'; 26 27 foreach($arrays as $key => $array){ 28 $cv = $array[$c]; 29 if(!isset($t[$cv])){ 30 $t[$cv] = []; 31 } 32 $t[$cv][] = $key; 33 } 34 ksort($t); 35 print_r($t); 36 foreach($t as $v){ 37 if(is_array($v)){ 38 foreach($v as $y){ 39 $s[$y] = $arrays[$y]; 40 } 41 }else{ 42 $s[$y] = $arrays[$y]; 43 } 44 } 45 print_r($s);
学习记录,方便复习