简体   繁体   中英

Sort multidimensional array by values

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>");

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.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM