php对于数组内某元素大小重新排列

Posted on Feb 9, 2015
 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
Array
(
    [0] => Array
        (
            [Name] => Anan Ryoko - Refrain.mp3
            [fullName] => Anan Ryoko - Refrain.mp3
            [length] => 6437585
            [uploadTime] => 1422975265
        )

    [1] => Array
        (
            [Name] => Andemund Orchestra - 梦中的婚礼.mp3
            [fullName] => Andemund Orchestra - 梦中的婚礼.mp3
            [length] => 1647776
            [uploadTime] => 1422975357
        )

    [2] => Array
        (
            [Name] => Ashram - Elizabeth.mp3
            [fullName] => Ashram - Elizabeth.mp3
            [length] => 1865495
            [uploadTime] => 1422975376
        )

    [3] => Array
        (
            [Name] => Bandari - 山溪.mp3
            [fullName] => Bandari - 山溪.mp3
            [length] => 3304113
            [uploadTime] => 1422975391
        )

)

我想对数组里的uploadTime进行重新排列,代码如下

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
<?php
array=Array
(
    '0' => Array
        (
            'Name' => 'Anan Ryoko - Refrain.mp3',
            'fullName' => 'Anan Ryoko - Refrain.mp3',
            'length' => '6437585',
            'uploadTime' => '1422975265',
        ),

    '1' => Array
        (
            'Name' => 'Andemund Orchestra - 梦中的婚礼.mp3',
            'fullName' => 'Andemund Orchestra - 梦中的婚礼.mp3',
            'length' => '1647776',
            'uploadTime' => '1422975357',
        ),

    '2' => Array
        (
            'Name' => 'Ashram - Elizabeth.mp3',
            'fullName' => 'Ashram - Elizabeth.mp3',
            'length' => '1865495',
            'uploadTime' => '1422975376',
        ),

    '3' => Array
        (
            'Name' => 'Bandari - 山溪.mp3',
            'fullName' => 'Bandari - 山溪.mp3',
            'length' => '3304113',
            'uploadTime' => '1422975391',
        ),
);
foreach(array as key =>row){  
    uploadTime[key] = row['uploadTime'];  
}  
array_multisort(uploadTime,SORT_DESC,array);
print_r (array);

输出

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
Array
(
    [0] => Array
        (
            [Name] => Bandari - 山溪.mp3
            [fullName] => Bandari - 山溪.mp3
            [length] => 3304113
            [uploadTime] => 1422975391
        )

    [1] => Array
        (
            [Name] => Ashram - Elizabeth.mp3
            [fullName] => Ashram - Elizabeth.mp3
            [length] => 1865495
            [uploadTime] => 1422975376
        )

    [2] => Array
        (
            [Name] => Andemund Orchestra - 梦中的婚礼.mp3
            [fullName] => Andemund Orchestra - 梦中的婚礼.mp3
            [length] => 1647776
            [uploadTime] => 1422975357
        )

    [3] => Array
        (
            [Name] => Anan Ryoko - Refrain.mp3
            [fullName] => Anan Ryoko - Refrain.mp3
            [length] => 6437585
            [uploadTime] => 1422975265
        )

)

完成~~

参考资料

w3schoolhttp://www.w3school.com.cn/php/func_array_multisort.asp

PHP Manualhttp://cn2.php.net/manual/zh/function.array-multisort.php