我在laravel中有以下收藏:
Collection {#357 ▼
#items: array:11 [▼
"29-04-2016" => array:2 [▼
"posTotal" => "100"
"posCount" => 1
]
"05-05-2016" => array:6 [▼
"posTotal" => "11"
"posCount" => 1
"keyedTotal" => "120"
"keyedCount" => 1
"cashTotal" => "32"
"cashCount" => 2
]
"10-05-2016" => array:10 [▼
"posTotal" => "67"
"posCount" => 4
"keyedTotal" => "22"
"keyedCount" => 1
"cashcardTotal" => "123"
"cashcardCount" => 1
"refundTotal" => "-50"
"refundCount" => 1
"cashRefundTotal" => "-10"
"cashRefundCount" => 1
]
"17-05-2016" => array:2 [▶]
"06-05-2016" => array:2 [▶]
"16-05-2016" => array:2 [▶]
"22-04-2016" => array:2 [▶]
"25-04-2016" => array:2 [▶]
]
}
现在我想按索引对它进行排序,但需要按日期将其转换。
例如,我想展示
"17-05-2016" => array:2 [▶]
"10-05-2016" => array:10 [▶]
"06-05-2016" => array:2 [▶]
等等...
我尝试过laravel的sort收集方法,还尝试了php的ksort函数将收集转换为数组。但这是一个字符串。
如果使用数组,则可以将uksort()与回调函数一起使用。您可以在其中编写自己的条件。
php代码:
// callback function
function cmp($a, $b){
if(strrev($a) == strrev($b)){
return 1;
}
return (strrev($a) < strrev($b)) ? -1 : 1;
}
// semple array
$test = array(
"05-06-2015" => "1",
"07-06-2016" => "3",
"05-08-2016" => "4",
"05-06-2016" => "2"
);
uksort( $test, "cmp" );
echo "<pre>";
print_r($test);
输出:
Array
(
[05-06-2015] => 1
[05-06-2016] => 2
[07-06-2016] => 3
[05-08-2016] => 4
)
本文收集自互联网,转载请注明来源。
如有侵权,请联系 [email protected] 删除。
我来说两句