对数字php数组排序

对数字php数组排序,php,arrays,sorting,Php,Arrays,Sorting,我有一个php数组$data,它包含一个文件列表 [3945] => 6358--338940.txt [3946] => 6357--348639.txt [3947] => 6356--348265.txt [3948] => 6354--345445.txt [3949] => 6354--340195.txt 我需要使用文件名中-之后的数值对数组进行排序。 怎么做 谢谢 关于如果需要算法,最好的方法是: 使用explode函数

我有一个php数组$data,它包含一个文件列表

   [3945] => 6358--338940.txt
   [3946] => 6357--348639.txt
   [3947] => 6356--348265.txt
   [3948] => 6354--345445.txt
   [3949] => 6354--340195.txt
我需要使用文件名中-之后的数值对数组进行排序。 怎么做

谢谢
关于

如果需要算法,最好的方法是:

使用explode函数提取数字并填充临时数组 将每个链转换为整数,以便在填充时使用intval函数更方便地进行排序 使用Sort函数对数组进行排序 以下是执行此操作的代码:

<?php
    /* your code here */

    $tempArray = [];

    foreach ($d as $data) {
        $value = explode("--", $d);
        $value = $value[1]; // Take the chain "12345.txt"
        $value = explode(".", $value);
        $value = $value[0]; // Take the chain "12345"
        $value = intval($value); // convert into integer

        array_push($tempArray, $value);
    }

    sort($value);
?>

你最好的选择是使用


使用自定义回调谢谢,你能解释一下使用自定义回调的含义吗?文档可以。谷歌搜索可以。我读过usort文档,但我找不到方法,如果我在这里,这意味着文档没有帮助。不,这意味着你很懒。
>>> $data
=> [
   3945 => "6358--338940.txt",
   3946 => "6357--348639.txt",
   3947 => "6356--348265.txt",
   3948 => "6354--345445.txt",
   3949 => "6354--340195.txt"
]
>>> uasort($data, function ($a, $b) { 
...   $pttrn = '#^[0-9]*--|\.txt$#';
...   $ka = preg_replace($pttrn, '', $a);
...   $kb = preg_replace($pttrn, '', $b);
...   return $ka > $kb;
... })
>>> $data 
=> [
   3945 => "6358--338940.txt",
   3949 => "6354--340195.txt",
   3948 => "6354--345445.txt",
   3947 => "6356--348265.txt",
   3946 => "6357--348639.txt"
 ]