Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Codeigniter sort by date array

i merge my array then i want to sort them by date but my loans and colls have different names in date

heres my code for the merging

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

and heres the output

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
    )
)

but i want to sort them by date.. any ideas?? thanks

like image 550
Zester Quinn Albano Avatar asked May 29 '17 10:05

Zester Quinn Albano


People also ask

How to sort a column by ascending order in CodeIgniter?

This codeigniter order by query explicitly specifies to sort the resultset using 'Author' column in ascending order. To sort a column by ascending order use asc as second parameter in order_by () method.

How to sort query resultset in CodeIgniter?

To sort query resultset in codeigniter you have to use order_by() method from 'Active Record Class' library. The method takes up two parameters, first one is the table column name and second one is optional and specifies if the column values should be sorted in ascending or decending order.

How to sort an array by value in PHP?

$array ( array) – The array to be sorted (passed by reference). $sortColumns ( array) – The array keys to sort after and the respective PHP sort flags as an associative array. Whether sorting was successful or not. This method sorts the elements of a multidimensional array by the values of one or more keys in a hierarchical way.

How to use model in CodeIgniter?

Using CodeIgniter’s Model ¶ 1 Models ¶. Models provide a way to interact with a specific table in your database. ... 2 Accessing Models ¶. Models are typically stored in the app/Models directory. ... 3 CodeIgniter’s Model ¶. ... 4 Creating Your Model ¶. ... 5 Working With Data ¶. ... 6 Model Events ¶. ... 7 Manual Model Creation ¶. ...


1 Answers

Hello yes you cant sort the arrays by date you need to use the function asort(if you want to do ascending sort), user arsort(if you want to do decending sort). here is the example that will make you understand.

$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>";
}
like image 102
Astound Avatar answered Oct 13 '22 13:10

Astound