0

How to sort following array in descending order by sorder? The whole array is considered to be one array for descending sorting.I see some other questions like this but none of them helped me.

    Array
(
    [0] => Array
        (
            [0] => Array
                (
                    [id] => 2208
                    [status] => u13333333333333
                    [user_id] => 6
                    [sorder] => 3

                )

            [1] => Array
                (
                    [id] => 2208
                    [status] => user111111111111111
                    [user_id] => 6
                    [sorder] => 1
                )

        )

    [1] => Array
        (
            [0] => Array
                (
                    [id] => 2209
                    [status] => u222222222222222222222
                    [user_id] => 5
                    [sorder] => 2

                )

        )

)

Edit May be this is another form of the array in two dimensional.

Array
(
    [userPosts] => Array
        (
            [0] => Array
                (
                    [0] => Array
                        (
                            [id] => 2208
                            [status] => u13333333333333
                            [user_id] => 2208
                            [sorder] => 3
                        )

                    [1] => Array
                        (
                            [id] => 2208
                            [status] => user111111111111111
                            [user_id] => 2208
                            [sorder] => 1

                        )

                )

            [1] => Array
                (
                    [0] => Array
                        (
                            [id] => 2209
                            [status] => u222222222222222222222
                            [user_id] => 2209
                            [sorder] => 2

                        )

                )

        )

)
7
  • Wouldn't you be better off combining the first and the second element so you have all three inner elements in a single array? You should be able to sort it after that. Commented Nov 7, 2013 at 20:48
  • Different id in this case... Commented Nov 7, 2013 at 20:48
  • @bibstha how can it be combine like that ? Commented Nov 7, 2013 at 20:51
  • 1
    @Jamal Either you only sort elements based on the innermost array (for instance, sort the innermost first two arrays between themselves and then the third innermost remains as it is) or you convert it to two dimentional array (combine all inner arrays under one parent) and sort it. How do you want it to be sorted? Commented Nov 7, 2013 at 20:58
  • @bibstha I updated the question, either of the solutions would be fine, I would prefer second solution. Commented Nov 7, 2013 at 21:06

2 Answers 2

3
$yourArray = array(

                array(
                        array('id' => 2208,
                                'status' => 'u13333333333333',
                                'user_id' => 6,
                                'sorder'=>3
                        ),
                        array('id' => 2208,
                                'status' => 'user111111111111111',
                                'user_id' => 6,
                                'sorder'=>1
                        ),
                ),
                array(
                        array('id' => 2209,
                                'status' => 'u222222222222222222222',
                                'user_id' => 5,
                                'sorder'=>2
                        ),

                ),


);
/*Merger arrays as one */
function loopArrayMerger(array $bigArray) {
    if (!$bigArray) {
        return array();
    }

    return call_user_func_array('array_merge', $bigArray);
}
$flatedArray = loopArrayMerger($yourArray);
/*Call back function for sorting, if you want in ascending order just replace 1:-1 with -1:1*/
function compareElementsInArray($a, $b) {
    if ($a['sorder'] == $b['sorder']) {
        return 0;
    }
    return ($a['sorder'] < $b['sorder']) ? 1 : -1;
}

/*Finally sort your array*/
uasort($flatedArray, 'compareElementsInArray');
echo '<pre>';
print_r($flatedArray);
Sign up to request clarification or add additional context in comments.

Comments

0

Do you want to maintain your two main arrays or merge them into one? Here's a solution for combining into one with something like this:

<?php

// merge branches of array
$array = array_merge($yourarray[0], $yourarray[1]);
$sorders = array();

foreach($array as $key => $current) {
    foreach($current as $subkey => $value) {
        // create new array with sorder as key, original key as value
        $sorders[$value['sorder']] = $subkey;   
    }
}

// sort by key (sorder)
ksort($sorders);

// create new array with ordered results
$new_array = array();
foreach($sorders as $sorder => $subkey) {

    // add values back
    $new_array[] = $array[0][$subkey];

}

print_r($new_array);

?>

Another question is how is this array being created initially? You may find that whatever is producing it (if it's a framework of some sort) might give you the option when retrieving the data to order by that field. If you're getting it with your own query from say MySQL, might be better to order by that field in your SQL query instead of doing it after...?

3 Comments

I have two users current user and his friends, the first array is current user's data and second array is his friends data (can be more than one). So I have to sort it here. Its not framework.
array_merge here has only two arrays but it may be up to n arrays like array_merge($yourarray[0], $yourarray[1]....$yourrray[n]);
take array_merge out of this example, pass your array into the first foreach and see if it works for you. If you have a name like userType as your first key, pass that into the foreach as well: foreach($yourarray['userType'] as ...

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.