0

I have below array with me:

Array(
[0] => Array(
     ['sort'] => 1
     ['ques'] => 'Zing order'
    )
[1] => Array(
     ['sort'] => 1
     ['ques'] => 'How stackoverflow works?'
    )
[2] => Array(
     ['sort'] => 2
     ['ques'] => 'What is PHP'
    )
)

What I want is:

Array(
    [0] => Array(
         ['sort'] => 1
         ['ques'] => 'How stackoverflow works?'
        )
    [1] => Array(
         ['sort'] => 1
         ['ques'] => 'Zing order'
        )
    [2] => Array(
         ['sort'] => 2
         ['ques'] => 'What is PHP'
        )
    )

Ideally, it should sort first with sort key and then alphabetically with ques key.

1 Answers1

0

You can use array_multisort() with the help of array_column(). So you have the power to set Sort Order now :)

<?php
$array = array(
    array(
        'sort' => 1,
        'ques' => 'Zing order'
    ),
    array(
        'sort' => 1,
        'ques' => 'How stackoverflow works?'
    ),
    array(
        'sort' => 2,
        'ques' => 'What is PHP'
    )
);
array_multisort(array_column($array, 'sort'), SORT_ASC,
                array_column($array, 'ques'),      SORT_ASC,
                $array);
print_r($array);
?>

DEMO: https://3v4l.org/ofeZG

A l w a y s S u n n y
  • 36,497
  • 8
  • 60
  • 103