0

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

2
Contributors
4
Replies
28
Views
3 Years
Discussion Span
Last Post by cereal
0

Hi,

This should do what you want:

<?php

$a = 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
    )
);

$qnumbers = array();
$result = array();

foreach($a as $key => $value)
{
    $qnumbers[$key] = $value['question_number'];
    asort($qnumbers, SORT_NUMERIC);
}


foreach($qnumbers as $key => $value)
{
    $result[] = $a[$key];
}

print_r($result);

The output is:

Array
(
    [0] => Array
        (
            [question_title] => Question 7
            [question_description] => 
            [type_id] => 1
            [type_title] => Yes/No
            [question_number] => 7
        )

    [1] => Array
        (
            [question_title] => Question 8
            [question_description] => 
            [type_id] => 1
            [type_title] => Yes/No
            [question_number] => 8
        )

    [2] => Array
        (
            [question_title] => Question 9
            [question_description] => 
            [type_id] => 1
            [type_title] => Yes/No
            [question_number] => 9
        )

    [3] => Array
        (
            [question_title] => Question 10
            [question_description] => 
            [type_id] => 1
            [type_title] => Yes/No
            [question_number] => 10
        )

    [4] => Array
        (
            [question_title] => Question 11
            [question_description] => 
            [type_id] => 1
            [type_title] => Yes/No
            [question_number] => 11
        )

)

Bye :)

0

Thanks, but that didn't help, I am still getting this result:
1
10
2
3
4
5
6
7
8
9

0

Ok, checked another option, and it looks like what's breaking the sort of the array is the jquery datatable plugin that I use. The array returned is in the right order, but dataTable breaks it.

0

This can happen when the keys are considered strings instead of integers. After generating the list try this:

$result = array_values($result);

And submit this to dataTable.

Edited by cereal

This topic has been dead for over six months. Start a new discussion instead.
Have something to contribute to this discussion? Please be thoughtful, detailed and courteous, and be sure to adhere to our posting rules.