Sorting an array based on a key

I have an array

echo '<pre>';print_r($assetArray);echo '</pre>';

which results in

Array
(
    [0] => Array
        (
            [name] => 
            [id] => 1
            [asset_type] => 1
            [chassis_row] => 1
            [chassis_col] => 1
            [manufacturer] => Brocade
            [model] => VDX 2730
        )

    [1] => Array
        (
            [name] => 
            [id] => 2
            [asset_type] => 1
            [chassis_row] => 6
            [chassis_col] => 1
            [manufacturer] => Brocade
            [model] => VDX 2730
        )

    [2] => Array
        (
            [name] => 
            [id] => 1
            [asset_type] => 2
            [chassis_row] => 3
            [chassis_col] => 1
            [manufacturer] => Aculab
            [model] => E1/T1 PCI
        )

    [3] => Array
        (
            [name] => 
            [id] => 2
            [asset_type] => 2
            [chassis_row] => 8
            [chassis_col] => 1
            [manufacturer] => Aculab
            [model] => E1/T1 PCI
        )

    [4] => Array
        (
            [name] => 
            [id] => 1
            [asset_type] => 4
            [chassis_row] => 4
            [chassis_col] => 1
            [manufacturer] => 3Com
            [model] => 3CB9LF10MC
        )

    [5] => Array
        (
            [name] => 
            [id] => 2
            [asset_type] => 4
            [chassis_row] => 5
            [chassis_col] => 1
            [manufacturer] => Unknown
            [model] => Unknown
        )

    [6] => Array
        (
            [name] => 
            [id] => 3
            [asset_type] => 3
            [chassis_row] => 2
            [chassis_col] => 1
            [manufacturer] => 3Com
            [model] => 3C16791C
        )

    [7] => Array
        (
            [name] => 
            [id] => 4
            [asset_type] => 3
            [chassis_row] => 7
            [chassis_col] => 1
            [manufacturer] => 3Com
            [model] => 3C16791C
        )

    [8] => Array
        (
            [name] => 
            [id] => 2
            [asset_type] => 5
            [chassis_row] => 9
            [chassis_col] => 1
            [manufacturer] => Unknown
            [model] => Unknown
        )

)

I’m trying to sort the array based on 2 keys (chassis_row and chassis_col) in ascending order so the new array is

Array
(
[0] => Array
(
[name] =>
[id] => 1
[asset_type] => 1
[chassis_row] => 1
[chassis_col] => 1
[manufacturer] => Brocade
[model] => VDX 2730
)

[1] => Array
    (
        [name] => 
        [id] => 3
        [asset_type] => 3
        [chassis_row] => 2
        [chassis_col] => 1
        [manufacturer] => 3Com
        [model] => 3C16791C
    )


[2] => Array
    (
        [name] => 
        [id] => 1
        [asset_type] => 2
        [chassis_row] => 3
        [chassis_col] => 1
        [manufacturer] => Aculab
        [model] => E1/T1 PCI
    )

[3] => Array
    (
        [name] => 
        [id] => 1
        [asset_type] => 4
        [chassis_row] => 4
        [chassis_col] => 1
        [manufacturer] => 3Com
        [model] => 3CB9LF10MC
    )

[4] => Array
    (
        [name] => 
        [id] => 2
        [asset_type] => 4
        [chassis_row] => 5
        [chassis_col] => 1
        [manufacturer] => Unknown
        [model] => Unknown
    )

[5] => Array
    (
        [name] => 
        [id] => 2
        [asset_type] => 1
        [chassis_row] => 6
        [chassis_col] => 1
        [manufacturer] => Brocade
        [model] => VDX 2730
    )
[6] => Array
    (
        [name] => 
        [id] => 4
        [asset_type] => 3
        [chassis_row] => 7
        [chassis_col] => 1
        [manufacturer] => 3Com
        [model] => 3C16791C
    )
[7] => Array
    (
        [name] => 
        [id] => 2
        [asset_type] => 2
        [chassis_row] => 8
        [chassis_col] => 1
        [manufacturer] => Aculab
        [model] => E1/T1 PCI
    )

[8] => Array
    (
        [name] => 
        [id] => 2
        [asset_type] => 5
        [chassis_row] => 9
        [chassis_col] => 1
        [manufacturer] => Unknown
        [model] => Unknown
    )

)

Quick question, are you getting these results from a database? If so, just create your query to SORT BY chassis_row and chassis_col.

If not, you always have usort as an option. They provide a multi-dimensional array example on that page too.

1 Like

I’m trying to use usort()

usort($assetArray,"asc");

but am having trouble with the callback function

function asc($a, $b)
{
  if($a['chassis_row'] > $b['chassis_row'])
  {  return 1; }
  else  if($a['chassis_row'] < $b['chassis_row'])
  {  return 1; }
  else
  { return 0; }

}

Is that on the right track as far as comparing the values of the key?

Return values should be 0 for equality, a negative numer if the second argument is smaller and a positive integer if the first argument is smaller. So returning 0, 1 or 1 is incorrect.

Fortunately, if you’re on PHP7 or higher you can use the spaceship operator:

function asc($a, $b)
{
    return $a['chassis_row'] <=> $b['chassis_row'];
}
1 Like