Hello,
I have an array that I need to sort by keys.
The array looks like this:
Array
(
[0] => Array
(
[question_title] => Question 8
[question_description] =>
[type_id] => 1
[type_title] => Yes/No
[question_number] => 8
)
[1] => Array
(
[question_title] => Question 9
[question_description] =>
[type_id] => 1
[type_title] => Yes/No
[question_number] => 9
)
[2] => Array
(
[question_title] => Question 10
[question_description] =>
[type_id] => 1
[type_title] => Yes/No
[question_number] => 10
)
[3] => Array
(
[question_title] => Question 11
[question_description] =>
[type_id] => 1
[type_title] => Yes/No
[question_number] => 11
)
[4] => Array
(
[question_title] => Question 7
[question_description] =>
[type_id] => 1
[type_title] => Yes/No
[question_number] => 7
)
)
I need to sort this array by question number, the problem is I get this result
1
10
11
2
3
I tried many many ways, nothing works.
For example I tried:
usort($questions, function($a, $b) {
return (int) $a['question_number'] - (int) $b['question_number'];
});
And:
foreach ($questions as $key => $row) {
$mykey = (int) $row['question_number'];
$volume[$mykey] = array(
'question_title'=>$row['question_title'],
'type_title'=>$row['type_title'],
'question_number'=>$row['question_number']
);
}
sort($volume, SORT_NUMERIC);
$questions = $volume;
And:
foreach ($questions as $key => $row) {
$mykey = (int) $row['question_number'];
$volume[$mykey] = array(
'question_title'=>$row['question_title'],
'type_title'=>$row['type_title'],
'question_number'=>$row['question_number']
);
}
array_multisort($volume, SORT_NUMERIC, $questions);
$questions = $volume;
Nothing works.
Any suggestions?
Thank you