PHP数组multisort [英] PHP array multisort
本文介绍了PHP数组multisort的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
我试图根据排序落键的数组,出动。但是,它不工作。有没有人有任何指针获得这个code的工作?感谢:
数组:
阵列
(
[0] =>排列
(
[wcccanumber] => 130700203
[电话] =>痫
[地址] => 221章第PINE ST
[县] => C
[站] => CNB
[科] =>坎比防火分区#62
[单位] => E61,M62
[派出] => 20时43分59秒
) [1] =>排列
(
[wcccanumber] => 130700198
[电话] => CARD / RESP逮捕
[地址] => 40781 HWY 26
[县] => C
[站] => SAN
[科] => SANDY防火分区#72
[单位] => 3709,CH37,M1,M272,R71
[派出] => 19时33分27秒
) [2] =>排列
(
[wcccanumber] => 130700337
[电话] =>外伤C1
[地址] => 16500 SW CENTURY DR
[县] => W¯¯
[站] => SHW
[科] =>图拉丁VALLEY FIRE&安培;拯救
[单位] => E33,METWA,MW68
[派出] => 21时40分13秒
) [3] =>排列
(
[wcccanumber] => 130700335
[电话] => FALL C1
[地址] => 48437 NW PONGRATZ RD
[县] => W¯¯
[站] => BUX
[科] =>银行防火分区#13
[单位] => E14,METWA,MW57,R13
[派出] => 21时07分48秒
))
code:
公共职能sortActiveCalls()
{
的foreach($这个 - > getActiveCalls()为$关键=> $ VAL){
$时间[$关键] = $ VAL ['派遣'];
} 在array_multisort($时间,SORT_ASC,$这个 - > getActiveCalls());
}
解决方案
我觉得它更容易只是使用usort
usort($阵列功能($ A,$ B){
返回的strtotime($ A [出动]) - 的strtotime($ B [出动]);
});
有关你的情况,你会重新实现你的 sortActiveCalls
方法
公共职能sortActiveCalls(){
$数据= $这个 - > getActiveCalls();
usort($的数据,功能($ A,$ B){
返回的strtotime($ A [出动]) - 的strtotime($ B [出动]);
});
返回$的数据;
}
不是这样只会在PHP 5.3及以上
工作如果您正在使用PHP的旧版本,您将必须定义一个单独的函数做有点像
如果(!function_exists(sortByDispatched)){
功能sortByDispatched($ A,$ B){
返回的strtotime($ A [出动]) - 的strtotime($ B [出动]);
}
}
usort($阵列sortByDispatched);
I am attempting to sort an array based off the key, "dispatched". However, it is not working. Does anyone have any pointers to get this code working? Thanks:
Array:
Array
(
[0] => Array
(
[wcccanumber] => 130700203
[call] => SEIZURES
[address] => 221 S PINE ST
[county] => C
[station] => CNB
[department] => CANBY FIRE DISTRICT #62
[units] => E61, M62
[dispatched] => 20:43:59
)
[1] => Array
(
[wcccanumber] => 130700198
[call] => CARD/RESP ARREST
[address] => 40781 HWY 26
[county] => C
[station] => SAN
[department] => SANDY FIRE DISTRICT #72
[units] => 3709, CH37, M1, M272, R71
[dispatched] => 19:33:27
)
[2] => Array
(
[wcccanumber] => 130700337
[call] => TRAUMA C1
[address] => 16500 SW CENTURY DR
[county] => W
[station] => SHW
[department] => TUALATIN VALLEY FIRE & RESCUE
[units] => E33, METWA, MW68
[dispatched] => 21:40:13
)
[3] => Array
(
[wcccanumber] => 130700335
[call] => FALL C1
[address] => 48437 NW PONGRATZ RD
[county] => W
[station] => BUX
[department] => BANKS FIRE DISTRICT #13
[units] => E14, METWA, MW57, R13
[dispatched] => 21:07:48
)
)
Code:
public function sortActiveCalls ()
{
foreach ($this->getActiveCalls() as $key => $val) {
$time[$key] = $val['dispatched'];
}
array_multisort($time, SORT_ASC, $this->getActiveCalls());
}
解决方案
I find it easier just to use usort
usort($array, function ($a, $b) {
return strtotime($a["dispatched"]) - strtotime($b["dispatched"]);
});
For your case you would reimplement your sortActiveCalls
method as
public function sortActiveCalls(){
$data = $this->getActiveCalls();
usort($data, function ($a, $b) {
return strtotime($a["dispatched"]) - strtotime($b["dispatched"]);
});
return $data;
}
Not this will only work in php 5.3 and up
If you are using an older version of php you will have to define a separate function to do the sort like
if (!function_exists("sortByDispatched")){
function sortByDispatched($a, $b){
return strtotime($a["dispatched"]) - strtotime($b["dispatched"]);
}
}
usort($array, "sortByDispatched");
这篇关于PHP数组multisort的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文