array sort php,PHP: sort - Manual

本文介绍了一种PHP函数stickysort,它允许对关联数组按指定字段进行排序,同时保持部分字段(如姓名)不变,实现在具有相同特定字段数据的记录组内部进行子排序。通过实例展示了如何使用此方法来实现复杂的数据组织和子级排序。
摘要由CSDN通过智能技术生成

This sort function allows you to sort an associative array while "sticking" some fields.

$sticky_fields = an array of fields that should not be re-sorted. This is a method of achieving sub-sorts within contiguous groups of records that have common data in some fields.

For example:

$a = array();

$a []= array(

'name'         => 'Sam',

'age'         => 23,

'hire_date'    => '2004-01-01'

);

$a []= array(

'name'        => 'Sam',

'age'        => 44,

'hire_date'    => '2003-03-23'

);

$a []= array(

'name'        => 'Jenny',

'age'        => 20,

'hire_date' => '2000-12-31'

);

$a []= array(

'name'        => 'Samantha',

'age'        => 50,

'hire_date' => '2000-12-14'

);

$sticky_fields = array( 'name' );

print_r( stickysort( $a, 'age', DESC_NUM, $sticky_fields ) );

OUTPUT:

Array

(

[0] => Array

(

[name] => Sam

[age] => 44

[hire_date] => 2003-03-23

)

[1] => Array

(

[name] => Sam

[age] => 23

[hire_date] => 2004-01-01

)

[2] => Array

(

[name] => Jenny

[age] => 20

[hire_date] => 2000-12-31

)

[3] => Array

(

[name] => Samantha

[age] => 50

[hire_date] => 2000-12-14

)

)

Here's why this is the correct output - the "name" field is sticky, so it cannot change its sort order. Thus, the "age" field is only sorted as a sub-sort within records where "name" is identical. Thus, the "Sam" records are reversed, because 44 > 23, but Samantha remains at the bottom, even though her age is 50. This is a way of achieving "sub-sorts" and "sub-sub-sorts" (and so on) within records of identical data for specific fields.

Courtesy of the $5 Script Archive: http://www.tufat.com

**/define('ASC_AZ',1000);define('DESC_AZ',1001);define('ASC_NUM',1002);define('DESC_NUM',1003);

functionstickysort($arr,$field,$sort_type,$sticky_fields= array() ) {$i=0;

foreach ($arras$value) {$is_contiguous=true;

if(!empty($grouped_arr)) {$last_value=end($grouped_arr[$i]);

if(!($sticky_fields== array())) {

foreach ($sticky_fieldsas$sticky_field) {

if ($value[$sticky_field] <>$last_value[$sticky_field]) {$is_contiguous=false;

break;

}

}

}

}

if ($is_contiguous)$grouped_arr[$i][] =$value;

else$grouped_arr[++$i][] =$value;

}$code='';

switch($sort_type) {

caseASC_AZ:$code.='return strcasecmp($a["'.$field.'"], $b["'.$field.'"]);';

break;

caseDESC_AZ:$code.='return (-1*strcasecmp($a["'.$field.'"], $b["'.$field.'"]));';

break;

caseASC_NUM:$code.='return ($a["'.$field.'"] - $b["'.$field.'"]);';

break;

caseDESC_NUM:$code.='return ($b["'.$field.'"] - $a["'.$field.'"]);';

break;

}$compare=create_function('$a, $b',$code);

foreach($grouped_arras$grouped_arr_key=>$grouped_arr_value)usort($grouped_arr[$grouped_arr_key],$compare);$arr= array();

foreach($grouped_arras$grouped_arr_key=>$grouped_arr_value)

foreach($grouped_arr[$grouped_arr_key] as$grouped_arr_arr_key=>$grouped_arr_arr_value)$arr[] =$grouped_arr[$grouped_arr_key][$grouped_arr_arr_key];

return$arr;

}?>

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值