Home > Backend Development > PHP Tutorial > 多维数组排序

多维数组排序

WBOYWBOYWBOYWBOYWBOYWBOYWBOYWBOYWBOYWBOYWBOYWBOYWB
Release: 2016-06-06 20:21:17
Original
1107 people have browsed it

我有个数组

<code>[
    ["id"=>3],
    ["id"=>1], 
    ["id"=>2]
]
</code>
Copy after login
Copy after login

现在要按照id的升序排列

<code>[
    ["id"=>1],
    ["id"=>2], 
    ["id"=>3]
]
</code>
Copy after login
Copy after login

我该怎么做呢?最好借用内置函数。

回复内容:

我有个数组

<code>[
    ["id"=>3],
    ["id"=>1], 
    ["id"=>2]
]
</code>
Copy after login
Copy after login

现在要按照id的升序排列

<code>[
    ["id"=>1],
    ["id"=>2], 
    ["id"=>3]
]
</code>
Copy after login
Copy after login

我该怎么做呢?最好借用内置函数。

http://php.net/manual/en/function.usort.php

<code>usort($arr, 'cmp');
function cmp($a, $b)
{
    if ($a['id'] == $b['id']) {
        return 0;
    }
    return ($a['id'] </code>
Copy after login

如果能确定value都是int型的话

<code>usort($arr, 'cmp');
function cmp($a, $b)
{
    return $a['id'] - $b['id'];
}</code>
Copy after login

<code>$data=当前数组;
foreach($data as $d){
    $arr[$data['id']]=$d;
}
print_r($arr);
</code>
Copy after login

你看下吧,写个函数,通用的,http://blog.csdn.net/igo9go_zq/article/details/48138405

Related labels:
php
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
Latest Issues
php data acquisition?
From 1970-01-01 08:00:00
0
0
0
PHP extension intl
From 1970-01-01 08:00:00
0
0
0
How to learn php well
From 1970-01-01 08:00:00
0
0
0
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template