如何根据以前的数字键对数组进行排序?

时间:2017-08-15 06:59:22

标签: php arrays sorting

我想对数组进行排序以重新索引其键。 我尝试使用自定义循环,但它不起作用。 有这样的PHP功能吗? 我有一个像这样的数组:

Array
(
    [3] => Array
        (
            [id] => 44
            [name] => 1495714823.jpg
        )
    [1] => Array
        (
            [id] => 48
            [name] => 1495795384.jpg
        )
    [2] => Array
        (
            [id] => 11
            [name] => 1495602099.jpg
        )
)

我需要像这样排序数组:

Array
(
    [1] => Array
        (
            [id] => 48
            [name] => 1495795384.jpg
        )
    [2] => Array
        (
            [id] => 11
            [name] => 1495602099.jpg
        )
    [3] => Array
        (
            [id] => 44
            [name] => 1495714823.jpg
        )
)

我尝试了一些排序功能,但它没有用。

1 个答案:

答案 0 :(得分:4)

我认为你需要strftime

java.lang.RuntimeException: Unable to start activity ComponentInfo{com.jafari.farhad.ubazdidcontroller/com.jafari.farhad.ubazdidcontroller.LoginActivity}: java.lang.NullPointerException
  at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2180)
  at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2230)
  at android.app.ActivityThread.access$600(ActivityThread.java:141)
  at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1234)
  at android.os.Handler.dispatchMessage(Handler.java:99)
  at android.os.Looper.loop(Looper.java:137)
  at android.app.ActivityThread.main(ActivityThread.java:5041)
  at java.lang.reflect.Method.invokeNative(Native Method)
  at java.lang.reflect.Method.invoke(Method.java:511)
  at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:793)
  at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:560)
  at dalvik.system.NativeStart.main(Native Method)
 Caused by: java.lang.NullPointerException
  at com.jafari.farhad.ubazdidcontroller.LoginActivity.onCreate(Unknown Source)
  at android.app.Activity.performCreate(Activity.java:5104)
  at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1080)
  at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2144)
  at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2230) 
  at android.app.ActivityThread.access$600(ActivityThread.java:141) 
  at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1234) 
  at android.os.Handler.dispatchMessage(Handler.java:99) 
  at android.os.Looper.loop(Looper.java:137) 
  at android.app.ActivityThread.main(ActivityThread.java:5041) 
  at java.lang.reflect.Method.invokeNative(Native Method) 
  at java.lang.reflect.Method.invoke(Method.java:511) 
  at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:793) 
  at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:560) 
  at dalvik.system.NativeStart.main(Native Method) 

这应该可以为您提供所需的输出 请参阅此处获取输出:ksort