foreach($ret as $key => $value) { //将数组元素中为空地补0,且添加sum元素的计算,注意修改数组的值,需要$key定位到是数组的哪一个地方需要修改,而不能简单地修改$value的值,因为$value只是一个临时的变量而已 $sum = 0; if (isset($value['new_amount'])) { $sum += $value['new_amount']; } else { //$value['new_amount'] = 0; //$value['new_amount'] = 0; $ret[$key]['new_amount'] = 0; $ret[$key]['new_amount'] = 0; } if (isset($value['level_10_amount'])) { $sum += $value['level_10_amount']; } else { //$value['level_10_amount'] = 0; //$value['level_10_amount'] = 0; $ret[$key]['level_10_amount'] = 0; $ret[$key]['level_10_amount'] = 0; } if (isset($value['continus_login_amount'])) { $sum += $value['continus_login_amount']; } else { //$value['continus_login_amount'] = 0; //$value['continus_login_amount'] = 0; $ret[$key]['continus_login_amount'] = 0; $ret[$key]['continus_login_amount'] = 0; } if ($sum % 10 != 0) { $sum = $sum + (10 - $sum % 10); } //$value['sum'] = $sum; $ret[$key]['sum'] = $sum; }
uasort($ret, 'my_sort'); //按元素sum进行排序 function my_sort($a, $b) { //排序 if ($a['sum'] == $b['sum']) return 0; return ($a['sum'] > $b['sum']) ? -1 : 1; //这里可以决定是倒序排,还是正序排 }
数组按照键id的值进行升序排序
<?php $array[] = array('id'=>1,'price'=>50); $array[] = array('id'=>2,'price'=>70); $array[] = array('id'=>3,'price'=>30); $array[] = array('id'=>4,'price'=>20); foreach ($array as $key=>$value){ $id[$key] = $value['id']; $price[$key] = $value['price']; } array_multisort($price,SORT_NUMERIC,SORT_DESC,$id,SORT_STRING,SORT_ASC,$array); ?>