Sort an array

I have

Array
(
    [0] => Array
        (
            [network_card_name] => fhjgsh
            [0] => fhjgsh
            [network_card_id] => 1
            [1] => 1
            [beginning_ru] => 42.0
            [2] => 42.0
            [ending_ru] => 45.0
            [3] => 45.0
            [beginning_x] => 0.0
            [4] => 0.0
        )

    [1] => Array
        (
            [network_panel_name] => hkl;
            [0] => hkl;
            [network_panel_id] => 1
            [1] => 1
            [beginning_ru] => 37.0
            [2] => 37.0
            [ending_ru] => 38.0
            [3] => 38.0
            [beginning_x] => 
            [4] => 
            [external_width] => 17.500
            [5] => 17.500
            [external_height] => 3.500
            [6] => 3.500
        )

    [2] => Array
        (
            [network_standard_name] => sfggjh
            [0] => sfggjh
            [network_standard_id] => 1
            [1] => 1
            [beginning_ru] => 34.0
            [2] => 34.0
            [ending_ru] => 34.9
            [3] => 34.9
            [beginning_x] => 32.1
            [4] => 32.1
            [external_width] => 8.386
            [5] => 8.386
            [external_height] => 1.575
            [6] => 1.575
        )

    [3] => Array
        (
            [server_name] => dgh gy
            [0] => dgh gy
            [server_id] => 1
            [1] => 1
            [beginning_ru] => 41.0
            [2] => 41.0
            [ending_ru] => 41.0
            [3] => 41.0
            [beginning_x] => 0.0
            [4] => 0.0
            [external_width] => 17.500
            [5] => 17.500
            [external_height] => 1.750
            [6] => 1.750
        )

    [4] => Array
        (
            [shelf_id] => 2
            [0] => 2
            [ending_ru] => 42.0
            [1] => 42.0
            [beginning_ru] => 42.0
            [2] => 42.0
        )

    [5] => Array
        (
            [periphial_storage_name] => jl;hkl
            [0] => jl;hkl
            [periphial_storage_id] => 1
            [1] => 1
            [beginning_ru] => 30.0
            [2] => 30.0
            [ending_ru] => 33.0
            [3] => 33.0
            [beginning_x] => 0.0
            [4] => 0.0
            [external_width] => 17.500
            [5] => 17.500
            [external_height] => 7.000
            [6] => 7.000
        )

)

Given its an array or arrays, how can I sort it by beginning_x?

  1. Get beginning_x with array_map().

  2. Sort two arrays together with array_multisort().

or just usort from the getgo.

You might also want to either just pull out records that don’t have your ‘beginning_x’ key or at least pull them out temporally so you don’t get errors sorting by that missing key, you can always throw them back in at the beginning or end of the sorted array. I did it this way (before seeing these suggestions) with $row being the array.

$noKEY = array_filter($row, function($check) {
    return $check = !array_key_exists('beginning_x',$check);
});

$hasKEY = array_filter($row, function($check) {
    return $check = array_key_exists('beginning_x',$check);
});

function compareByField($a, $b) {
  return strcmp($a["beginning_x"], $b["beginning_x"]);
}

uasort($hasKEY, 'compareByField');

$sorted = $noKEY + $hasKEY;

This topic was automatically closed 91 days after the last reply. New replies are no longer allowed.