Home > php教程 > php手册 > 【PHP】php 二维关联数组的对某一个值排序

【PHP】php 二维关联数组的对某一个值排序

WBOY
Release: 2016-06-06 19:45:10
Original
1292 people have browsed it

$arr=Array ( [0] =Array ( [id] =169, [sorts] =4, [fans] = 0, [created_at] =1413621597 ) [1] =Array ( [id] =170, [sorts] =5, [fans] = 5, [created_at] =1418621597 ) [2] =Array ( [id] =171, [sorts] =1, [fans] = 9, [created_at] =1413621597 ) )

$arr=Array
(
    [0] =Array
        (
            [id] =169,
            [sorts] =4,
            [fans] = 0,      
            [created_at] =1413621597
        )


    [1] =Array
        (
            [id] =170,
            [sorts] =5,
            [fans] = 5,      
            [created_at] =1418621597
        )


    [2] =Array
        (
            [id] =171,
            [sorts] =1,
            [fans] = 9,      
            [created_at] =1413621597
        )

)

$arr是一个二维的关联数组,要对sorts的值按顺序/倒叙排序,需要用到array_multisort( )这个函数。

这个函数第一个参数是数组(必填,后面参数可选),第二个是排序方式SORT_ASC默认升序,SORT_DESC 降序,第三个SORT_REGULAR默认将每一项按常规顺序排列,SORT_NUMERIC按数字顺序排列,SORT_STRING按字母顺序排列,第四个是外层数组$arr

foreach ($arr as $key => $value) {

    $temp[$key]=$value['sorts'];
  }

array_multisort($temp,SORT_ASC,$arr);

print_r($arr);即可

Related labels:
source:php.cn
Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
Popular Recommendations
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template