phparrayssorting

Custom sort the keys of a flat associative array


I'm having an array of data like this:

$array = array(
'total_ids' => 0,
'unique_ips' => 0,
'unique_ids' => 0,
'global' => 0,
'total_ips' => 0,
);

I need it to be sorted to this:

$array = array(
'unique_ids' => 0,
'unique_ips' => 0,
'total_ids' => 0,
'total_ips' => 0,
'global' => 0
);

I believe this can be done via uksort, but I can't find the solution of custom_sort function.


Solution

  • This seems rather pointless, can you provide a reason why you need this? I assume it's something to do with a looped output.

    $new_array = array(
      'unique_ids' => $array['unique_ids'],
      'unique_ips' => $array['unique_ips'],
      'total_ids' => $array['total_ids'],
      'total_ips' =>$array['total_ips'],
      'global' => $array['global']
    );
    $array = $new_array;