2023-05-12 开启多语言插件支持……

php array_multisort 多维数组排序

php 苏 demo 2913℃ 0评论

array_multisort — 对多个数组或多维数组进行排序

排序顺序标志:

  • SORT_ASC – 按照上升顺序排序
  • SORT_DESC – 按照下降顺序排序

排序类型标志:

  • SORT_REGULAR – 将项目按照通常方法比较
  • SORT_NUMERIC – 将项目按照数值比较
  • SORT_STRING – 将项目按照字符串比较

有些抽象, 不好描述, 我给出我实验的例子,  你琢磨琢磨吧:
先按照时间降序, 时间相同的话, 再按照手机号码降序排列
<?php
$data = array(array(‘time’ => ‘2009-03-11’, ‘mobile’ => ‘152****8888’, ‘award’ => ‘PHP’),);

$tmp1 = array(‘time’ => ‘2009-03-12’, ‘mobile’ => ‘151****0257’, ‘award’ => ‘2元话费’);
$tmp2 = array(‘time’ => ‘2009-03-13’, ‘mobile’ => ‘151****5941’, ‘award’ => ’20元话费’);
$tmp3 = array(‘time’ => ‘2009-03-11’, ‘mobile’ => ‘150****7022’, ‘award’ => ‘800纹银’);
$tmp4 = array(‘time’ => ‘2009-03-11’, ‘mobile’ => ‘159****3894’, ‘award’ => ’30充值卡’);
$tmp5 = array(‘time’ => ‘2009-03-11’, ‘mobile’ => ‘152****5243’, ‘award’ => ‘200纹银’);
$tmp6 = array(‘time’ => ‘2009-03-11’, ‘mobile’ => ‘134****9341’, ‘award’ => ‘100纹银’);
$tmp7 = array(‘time’ => ‘2009-03-11’, ‘mobile’ => ‘150****0374’, ‘award’ => ‘4元话费’);
$tmp8 = array(‘time’ => ‘2009-03-11’, ‘mobile’ => ‘152****9107’, ‘award’ => ‘5纹银’);
$tmp9 = array(‘time’ => ‘2009-03-12’, ‘mobile’ => ‘151****3791’, ‘award’ => ‘1纹银’);
$tmp10 = array(‘time’ => ‘2009-03-11’, ‘mobile’ => ‘152****4676’, ‘award’ => ’30充值卡’);
array_push($data,$tmp1,$tmp2,$tmp3,$tmp4,$tmp5,$tmp6,$tmp7,$tmp8,$tmp9,$tmp10);

//print_r($data);

foreach($data as $key => $value)
{
$time[$key] = $value[‘time’];
$mobile[$key] = $value[‘mobile’];
}

array_multisort($time,SORT_DESC,$mobile,SORT_DESC,$data);
//print_r($data);

打赏

转载请注明:苏demo的别样人生 » php array_multisort 多维数组排序

   如果本篇文章对您有帮助,欢迎向博主进行赞助,赞助时请写上您的用户名。
支付宝直接捐助帐号oracle_lee@qq.com 感谢支持!
喜欢 (0)or分享 (0)