Skip to main content
 首页 » 编程设计

php之Codeigniter 按日期数组排序

2024年05月29日18虾米姐

我合并我的数组,然后我想按日期对它们进行排序,但我的贷款和科尔斯在日期中有不同的名称

这是我的合并代码

 $loan = $this->db->get('loans')->result_array(); 
 $coll = $this->db->get('collectables')->result_array();        
 $result = array_merge($loan, $coll); 

这是输出

Array 
( 
[0] => Array 
    ( 
        [loan_id] => 175 
        [loan_fullname] => Albano, Zester Quinn 
        [loan_amount] => 15000 
        [loan_interest] => 2 
        [loan_date] => 2017-05-30 
        [loan_total_amount] => 15300 
        [loan_collectables] => 1 
        [loan_user_id] => 30 
    ) 
 
[1] => Array 
    ( 
        [loan_id] => 176 
        [loan_fullname] => Amamio, Alyanna 
        [loan_amount] => 15000 
        [loan_interest] => 2 
        [loan_date] => 2017-05-31 
        [loan_total_amount] => 15300 
        [loan_collectables] => 2 
        [loan_user_id] => 32 
    ) 
 
[2] => Array 
    ( 
        [coll_id] => 92 
        [coll_date] => 2017-05-30 
        [coll_amount] => 15300 
        [coll_loan_id] => 175 
        [coll_user_id] => 30 
    ) 
 
[3] => Array 
    ( 
        [coll_id] => 93 
        [coll_date] => 2017-05-28 
        [coll_amount] => 7650 
        [coll_loan_id] => 176 
        [coll_user_id] => 32 
    ) 
 
 [4] => Array 
    ( 
        [coll_id] => 94 
        [coll_date] => 2017-06-21 
        [coll_amount] => 7650 
        [coll_loan_id] => 176 
        [coll_user_id] => 32 
    ) 
) 

但我想按日期对它们进行排序..有什么想法吗?谢谢

请您参考如下方法:

你好,是的,你不能按日期对数组进行排序,你需要使用函数asort(如果你想做升序排序),用户arsort(如果你想做降序排序)。这是让您理解的示例。

$age = array("Peter"=>"2017-05-30", "Ben"=>"2017-01-31", "Joe"=>"2017-05-30"); 
asort($age); 
foreach($age as $x => $x_value) { 
    echo "Key=" . $x . ", Value=" . $x_value; 
    echo "<br>"; 
}