vimal kumar
vimal kumar

Reputation: 325

Array sorting in php using index

This is my array i need to get the array in reverse order how to sort it in the reverse order I need to sort it by using the index of the array In the following array it has tow index [0] and [1] I need to sort it in reverse and after sorting index [0] should show the records of [1] and [1] should show the records of [0]

I have shown my array in the following help me to solve it

Array
(
    [0] => Array
        (
            [0] => stdClass Object
                (
                    [user_id] => 103
                    [full_name] => newuser
                    [mobile_number] => 4152638596
                    [country] => 
                    [email] => [email protected]
                    [password] => MTIzNDU2
                    [secret_pin] => MTIzNDU2
                    [date_of_join] => 2016-10-14 13:52:38
                    [status] => 1
                    [email_verification] => 1
                    [bitcoin_address] => mhvnsceqMHoQqv9vDfXc7Y97d58R8CWA5S
                    [user_type] => 1
                    [last_login] => 2016-10-15 10:44:02.000000
                    [logged_in] => 1
                    [otp_code] => 0
                    [reference_code] => 597711
                    [direct_id] => 1
                    [parent_id] => 1
                    [root_id] => 1
                    [due_time] => 2016-10-15 13:52:38
                    [badge] => 2
                )

        )

    [1] => Array
        (
            [0] => stdClass Object
                (
                    [user_id] => 1
                    [full_name] => admin
                    [mobile_number] => 8252525263
                    [country] => 
                    [email] => [email protected]
                    [password] => MTIzNDU2
                    [secret_pin] => MTIzNDU2
                    [date_of_join] => 2016-10-07 17:51:24
                    [status] => 1
                    [email_verification] => 1
                    [bitcoin_address] => mhvnsceqMHoQqv9vDfXc7Y97d58R8CWA5S
                    [user_type] => 2
                    [last_login] => 2016-10-12 10:46:35.000000
                    [logged_in] => 1
                    [otp_code] => 0
                    [reference_code] => 111111
                    [direct_id] => 0
                    [parent_id] => 0
                    [root_id] => 0
                    [due_time] => 2016-10-12 09:24:18
                    [badge] => 1
                )

        )

)

var_export() results shown below

array (
  0 => 
  array (
    0 => 
    stdClass::__set_state(array(
       'user_id' => '103',
       'full_name' => 'newuser',
       'mobile_number' => '4152638596',
       'country' => '',
       'email' => '[email protected]',
       'password' => 'MTIzNDU2',
       'secret_pin' => 'MTIzNDU2',
       'date_of_join' => '2016-10-14 13:52:38',
       'status' => '1',
       'email_verification' => '1',
       'bitcoin_address' => 'mhvnsceqMHoQqv9vDfXc7Y97d58R8CWA5S',
       'user_type' => '1',
       'last_login' => '2016-10-15 10:44:02.000000',
       'logged_in' => '1',
       'otp_code' => '0',
       'reference_code' => '597711',
       'direct_id' => '1',
       'parent_id' => '1',
       'root_id' => '1',
       'due_time' => '2016-10-15 13:52:38',
       'badge' => '2',
    )),
  ),
  1 => 
  array (
    0 => 
    stdClass::__set_state(array(
       'user_id' => '1',
       'full_name' => 'admin',
       'mobile_number' => '8252525263',
       'country' => '',
       'email' => '[email protected]',
       'password' => 'MTIzNDU2',
       'secret_pin' => 'MTIzNDU2',
       'date_of_join' => '2016-10-07 17:51:24',
       'status' => '1',
       'email_verification' => '1',
       'bitcoin_address' => 'mhvnsceqMHoQqv9vDfXc7Y97d58R8CWA5S',
       'user_type' => '2',
       'last_login' => '2016-10-12 10:46:35.000000',
       'logged_in' => '1',
       'otp_code' => '0',
       'reference_code' => '111111',
       'direct_id' => '0',
       'parent_id' => '0',
       'root_id' => '0',
       'due_time' => '2016-10-12 09:24:18',
       'badge' => '1',
    )),
  ),
)

Upvotes: 2

Views: 86

Answers (1)

Rejoanul Alam
Rejoanul Alam

Reputation: 5398

Your solution is array_reverse() http://php.net/manual/en/function.array-reverse.php

$arr = array (
  0 => 
  array (
    0 => 
    array(
       'user_id' => '103',
       'full_name' => 'newuser',
       'mobile_number' => '4152638596',
       'country' => '',
       'email' => '[email protected]',
       'password' => 'MTIzNDU2',
       'secret_pin' => 'MTIzNDU2',
       'date_of_join' => '2016-10-14 13:52:38',
       'status' => '1',
       'email_verification' => '1',
       'bitcoin_address' => 'mhvnsceqMHoQqv9vDfXc7Y97d58R8CWA5S',
       'user_type' => '1',
       'last_login' => '2016-10-15 10:44:02.000000',
       'logged_in' => '1',
       'otp_code' => '0',
       'reference_code' => '597711',
       'direct_id' => '1',
       'parent_id' => '1',
       'root_id' => '1',
       'due_time' => '2016-10-15 13:52:38',
       'badge' => '2',
    ),
  ),
  1 => 
  array (
    0 => 
    array(
       'user_id' => '1',
       'full_name' => 'admin',
       'mobile_number' => '8252525263',
       'country' => '',
       'email' => '[email protected]',
       'password' => 'MTIzNDU2',
       'secret_pin' => 'MTIzNDU2',
       'date_of_join' => '2016-10-07 17:51:24',
       'status' => '1',
       'email_verification' => '1',
       'bitcoin_address' => 'mhvnsceqMHoQqv9vDfXc7Y97d58R8CWA5S',
       'user_type' => '2',
       'last_login' => '2016-10-12 10:46:35.000000',
       'logged_in' => '1',
       'otp_code' => '0',
       'reference_code' => '111111',
       'direct_id' => '0',
       'parent_id' => '0',
       'root_id' => '0',
       'due_time' => '2016-10-12 09:24:18',
       'badge' => '1',
    ),
  ),
);
$k = array_reverse($arr);
echo '<pre>';
print_r($k);

Run it and see your desired result. I have removed stdClass for test. You may run your own

Upvotes: 3

Related Questions