0

I have the multidimensional array and I'm trying to sort it by the values in each array.

I have not really sorted multidimensional arrays by a value within it and had no joy myself.

I have given the array below and example of the output when sorted by a value within the array.

Array

$list = array (
        0 => array(
                0 => '<input class="id" name="id[]" type="checkbox" value="7">',
                1 => '7',
                2 => '17',
                3 => 'Group Name',
                4 => 'Katie-Lee',
                5 => 'Homes',
                6 => 'b@b.com'
        ),
        1 => array(
                0 => '<input class="id" name="id[]" type="checkbox" value="22">',
                1 => '22',
                2 => '17',
                3 => 'Group Name',
                4 => 'John',
                5 => 'Collins',
                6 => 'a@a.com',
        ),
        2 => array(
                0 => '<input class="id" name="id[]" type="checkbox" value="9">',
                1 => '9',
                2 => '17',
                3 => 'Group Name',
                4 => 'Rob',
                5 => 'Smith',
                6 => 'rob@smith.com',
        ),
        3 => array(
                0 => '<input class="id" name="id[]" type="checkbox" value="1">',
                1 => '1',
                2 => '17',
                3 => 'Group Name',
                4 => 'Claire',
                5 => 'Taylor',
                6 => 'claire.taylor@example.com',
        ),
);

Sort by key $list[][1] (id)

$list = array (
    0 => array(
                0 => '<input class="id" name="id[]" type="checkbox" value="1">',
                1 => '1', // <-- Sort by id
                2 => '17',
                3 => 'Group Name',
                4 => 'Claire',
                5 => 'Taylor',
                6 => 'claire.taylor@example.com',
        ),
    1 => array(
                0 => '<input class="id" name="id[]" type="checkbox" value="7">',
                1 => '7', // <-- Sort by id
                2 => '17',
                3 => 'Group Name',
                4 => 'Katie-Lee',
                5 => 'Homes',
                6 => 'b@b.com'
        ), 
    2 => array(
                0 => '<input class="id" name="id[]" type="checkbox" value="9">',
                1 => '9', // <-- Sort by id
                2 => '17',
                3 => 'Group Name',
                4 => 'Rob',
                5 => 'Smith',
                6 => 'rob@smith.com',
        ),
    3 => array(
                0 => '<input class="id" name="id[]" type="checkbox" value="22">',
                1 => '22', // <-- Sort by id
                2 => '17',
                3 => 'Group Name',
                4 => 'John',
                5 => 'Collins',
                6 => 'a@a.com',
        ), 
);

Sort by key $list[][6] (email)

$list = array (
    0 => array(
                0 => '<input class="id" name="id[]" type="checkbox" value="22">',
                1 => '22',
                2 => '17',
                3 => 'Group Name',
                4 => 'John',
                5 => 'Collins',
                6 => 'a@a.com', // <-- Sort by email
        ),
        1 => array(
                0 => '<input class="id" name="id[]" type="checkbox" value="7">',
                1 => '7',
                2 => '17',
                3 => 'Group Name',
                4 => 'Katie-Lee',
                5 => 'Homes',
                6 => 'b@b.com' // <-- Sort by email
        ),
    2 => array(
                0 => '<input class="id" name="id[]" type="checkbox" value="1">',
                1 => '1',
                2 => '17',
                3 => 'Group Name',
                4 => 'Claire',
                5 => 'Taylor',
                6 => 'claire.taylor@example.com', // <-- Sort by email
        ), 
        3 => array(
                0 => '<input class="id" name="id[]" type="checkbox" value="9">',
                1 => '9',
                2 => '17',
                3 => 'Group Name',
                4 => 'Rob',
                5 => 'Smith',
                6 => 'rob@smith.com', // <-- Sort by email
        ),

);

Any help would be great, thanks.

UPDATE

I have updated with the code below to show it working for anyone else.

$list = array (
        0 => array(
                0 => '<input class="id" name="id[]" type="checkbox" value="7">',
                1 => '7',
                2 => '17',
                3 => 'Group Name',
                4 => 'Katie-Lee',
                5 => 'Homes',
                6 => 'b@b.com'
        ),
        1 => array(
                0 => '<input class="id" name="id[]" type="checkbox" value="22">',
                1 => '22',
                2 => '17',
                3 => 'Group Name',
                4 => 'John',
                5 => 'Collins',
                6 => 'a@a.com',
        ),
        2 => array(
                0 => '<input class="id" name="id[]" type="checkbox" value="9">',
                1 => '9',
                2 => '17',
                3 => 'Group Name',
                4 => 'Rob',
                5 => 'Smith',
                6 => 'rob@smith.com',
        ),
        3 => array(
                0 => '<input class="id" name="id[]" type="checkbox" value="1">',
                1 => '1',
                2 => '17',
                3 => 'Group Name',
                4 => 'Claire',
                5 => 'Taylor',
                6 => 'claire.taylor@example.com',
        ),
);

echo 'Array before sort:';
print("<pre>" . print_r($list, true). "</pre>");

$sortField = 6; // the id 

usort($list, function($a, $b) use ($sortField) 
{
        return strnatcmp($a[$sortField], $b[$sortField]);
});

echo 'Array after sort:';
print("<pre>" . print_r($list, true). "</pre>");
arbme
  • 4,831
  • 11
  • 44
  • 57
  • possible duplicate of [How do I sort a multidimensional array in php](http://stackoverflow.com/questions/96759/how-do-i-sort-a-multidimensional-array-in-php) – Jon Jul 02 '13 at 22:33
  • possible duplicate of [Reference: all basic ways to sort arrays in PHP](http://stackoverflow.com/questions/17364127/reference-all-basic-ways-to-sort-arrays-in-php) – Ja͢ck Jul 02 '13 at 23:17

1 Answers1

0

If you are using php 5.3+ you can use usort and a closure.

$sortField = 1; // the id 

usort($array, function($a, $b) use ($sortField) {
      return strnatcmp($a[$sortField], $b[$sortField]);
});

With this you will be able to sort the array based on which ever field is specified in the sortField variable.

Orangepill
  • 24,500
  • 3
  • 42
  • 63