How to sort multidimensional array

Hello!

I have array:

Array
(
    [list] => Array
        (
            [0] => Array
                (
                    [transaction_list] => Array
                        (
                            [0] => Array
                                (
                                    [category] => Array
                                        (
                                            [hierarchy] => Array
                                                (
                                                    [0] => Array
                                                        (
                                                            [id] => 2
                                                            [title] => some title
                                                        )
                                                )
                                        )
                                )
                         
                            [2] => Array
                                (
                                    [category] => Array
                                        (
                                            [hierarchy] => Array
                                                (
                                                    [0] => Array
                                                        (
                                                            [id] => 1
                                                            [title] => some title
                                                        )

                                                    [1] => Array
                                                        (
                                                            [id] => 11
                                                            [title] => some title
                                                        )
                                                )
                                        )
                                )

                            [3] => Array
                                (
                                    [category] => Array
                                        (
                                            [hierarchy] => Array
                                                (
                                                    [0] => Array
                                                        (
                                                            [id] => 2
                                                            [title] => some title
                                                        )

                                                    [1] => Array
                                                        (
                                                            [id] => 12
                                                            [title] => some title
                                                        )
                                                )
                                        )
                                )
                        )
                )
        )
)

I need to sort it by first id in hierarchy so the output be:

Array
(
    [list] => Array
        (
            [0] => Array
                (
                    [transaction_list] => Array
                        (
						
                            [1] => Array
                                (
                                    [category] => Array
                                        (
                                            [hierarchy] => Array
                                                (
                                                    [0] => Array
                                                        (
                                                            [id] => 1
                                                            [title] => some title
                                                        )

                                                    [1] => Array
                                                        (
                                                            [id] => 11
                                                            [title] => some title
                                                        )
                                                )
                                        )
                                )						
						
                            [2] => Array
                                (
                                    [category] => Array
                                        (
                                            [hierarchy] => Array
                                                (
                                                    [0] => Array
                                                        (
                                                            [id] => 2
                                                            [title] => some title
                                                        )
                                                )
                                        )
                                )
                         


                            [3] => Array
                                (
                                    [category] => Array
                                        (
                                            [hierarchy] => Array
                                                (
                                                    [0] => Array
                                                        (
                                                            [id] => 2
                                                            [title] => some title
                                                        )

                                                    [1] => Array
                                                        (
                                                            [id] => 12
                                                            [title] => some title
                                                        )
                                                )
                                        )
                                )
                        )
                )
        )
)

I have tried usort, ausort, ksort some custom methods found in online. … But nothing :frowning:

How to achieve this?

http://php.net/array-multisort ?

Tried that one too! Do not know what i m doing wrong but nothing seems to be working for me. … Half day spent already :frowning:

What exactly have you tried? Using array_multisort() may require some setup before it does what you want.

i tried examples from php.net mostly and tried to modify them with no success

Last i tried:

//$random_array << my array

function sortArray( $data, $field ) {
    $field = (array) $field;
    uasort( $data, function($a, $b) use($field) {
        $retval = 0;
        foreach( $field as $fieldname ) {
            if( $retval == 0 ) $retval = strnatcmp( $a[$fieldname], $b[$fieldname] );
        }
        return $retval;
    } );
    return $data;
}

$data = sortArray( $random_array, 'id' );
$data = sortArray( $random_array, array( 'list', 'transaction_list', 'category', 'hierarchy' ) );

If you want to sort on the 4th level, you have to pass the 4th level to the sort function.

This part is where i m stuck. … How to get till 4th level?

$array[<level-1>][<level-2>][<level-3>][<level-4>]

Funny. …

I need it to be automatic not hardcoded.

i can get all data from my array with foreach() and for() just order is not wanted.

I didn’t say that you have to hard-code it.

This topic was automatically closed 91 days after the last reply. New replies are no longer allowed.