PHP按键名排序数组

时间:2015-05-28 10:22:13

标签: php arrays sorting

我有一个像这样的数组

//$src
Array(
[dim_55] => path/to/image.jpg
[dim_180] => path/to/image.jpg
[dim_190] => path/to/image.jpg
[dim_475] => path/to/image.jpg
[dim_635] => path/to/image.jpg
[dim_540] => path/to/image.jpg
[dim_1130] => path/to/image.jpg
[dim_1900] => path/to/image.jpg
)

path/to/image.jpg是通用路径)

我想通过key按照

这样的顺序订购
dim_1900
dim_1130
dim_635
dim_540
dim_475
..

为了实现这个解决方案,我做了类似的事情:

$sortSrc = array();
foreach($src as $key => $value){
  $newkey = explode("dim_", $key);
  $sortSrc[$newkey[1]] = $value; 
}

krsort($sortSrc);

它有效但可能不那么有效。有没有一种方法可以通过本地PHP函数实现这一目标?

4 个答案:

答案 0 :(得分:4)

您需要使用krsort($src, SORT_NATURAL);

请参阅:http://php.net/manual/en/function.sort.php了解排序标记文档

答案 1 :(得分:0)

你应该能够用ksort达到你想要的效果:

api.clients(token, date, new Callback<List<Client>>() { new Callback<LoginResponse>() { @Override public void success(List<Client> clients, Response response) { Logs.retrofit("Clients status: " + response.getStatus()); if (response.getStatus() == 200) { //It's all good man! saveClients(clients); if (callbacks != null) callbacks.onClientListDownloadSuccessful(); } else if (response.getStatus() == 401) { //Wrong token, refresh it and retry refreshLoginToken(); } else { //Other error status if (callbacks != null) callbacks.onClientListDownloadError(response.getStatus()); } } @Override public void failure(RetrofitError error) { Logs.retrofit("Client error: " + error.getMessage()); if (error.getResponse() != null && error.getResponse() .getStatus() == 401) { refreshLoginToken(); } else if (callbacks != null) callbacks.onClientListDownloadError(0); } private void saveClients(List<Client> clients) { Logs.realm("Saving clients to local db: " + clients.size()); Realm realm = App.getCommercialiRealm(context); List<Long> ids = new ArrayList<>(); realm.beginTransaction(); for (Client client : clients) { if (client.isDeleted()) { //Delete client ids.add(client.getID()); } } //Add or update clients realm.copyToRealmOrUpdate(clients); Logs.realm("Client in db " + realm.where(Client.class) .count()); //Delete clients deleteClients(realm, ids); Logs.realm("Client in db after cleanup " + realm.where(Client.class) .count()); realm.commitTransaction(); PrefsUtils.saveLastSync(context, System.currentTimeMillis()); }

krsort — Sort an array by key in reverse order

以上示例将输出:

 <?php
$fruits = array("d"=>"lemon", "a"=>"orange", "b"=>"banana", "c"=>"apple");
krsort($fruits);
foreach ($fruits as $key => $val) {
    echo "$key = $val\n";
}
?>

答案 2 :(得分:0)

您还可以使用带有uksort()功能的键数字进行自定义排序,请尝试使用此代码

<?php 
$data = Array(
"dim_55" => "path/to/image.jpg",
"dim_180" => "path/to/image.jpg",
"dim_190" => "path/to/image.jpg",
"dim_475" => "path/to/image.jpg",
"dim_635" => "path/to/image.jpg",
"dim_540" => "path/to/image.jpg",
"dim_1130" => "path/to/image.jpg",
"dim_1900" => "path/to/image.jpg"
);
$sort_data = uksort($data, function($a, $b){
  preg_match("/dim_(\d+)/",$a, $matcha);
  preg_match("/dim_(\d+)/",$b, $matchb);
  if($matcha[1] == $matchb[1]){
    return 0;
  }
  return $matcha[1] > $matchb[1] ? -1 : 1;
});
print_r($data);
?>

DEMO

答案 3 :(得分:0)

#!/usr/bin/env php
<?php
$a = [
    'dim_55' => 'path/to/image.jpg',
    'dim_180' => 'path/to/image.jpg',
    'dim_190' => 'path/to/image.jpg',
    'dim_475' => 'path/to/image.jpg',
    'dim_635' => 'path/to/image.jpg',
    'dim_540' => 'path/to/image.jpg',
    'dim_1130' => 'path/to/image.jpg',
    'dim_1900' => 'path/to/image.jpg',
];

krsort($a, SORT_NATURAL);

print_r($a);