2012-07-18 117 views
0

我想如下排序多维数组键

<?php 
$country = array(
    array(
     'country' => 'India', 
     'visits' => 22, 
     'newVisits' => 16, 
     'newVisitsPercent' => 72.7), 
    array(
     'country' => 'USA', 
     'visits' => 30, 
     'newVisits' => 15, 
     'newVisitsPercent' => 50), 
    array(
     'country' => 'Japan', 
     'visits' => 25, 
     'newVisits' => 15, 
     'newVisitsPercent' => 60)); 
?> 

排序多维数组基于特定键值的数组,我想排序数组中的降序的“访问”的顺序数组的键。

所需的阵列是

<?php 
$country = array(
    array(
     'country' => 'USA', 
     'visits' => 30, 
     'newVisits' => 15, 
     'newVisitsPercent' => 50), 
    array(
     'country' => 'Japan', 
     'visits' => 25, 
     'newVisits' => 15, 
     'newVisitsPercent' => 60), 
    array(
     'country' => 'India', 
     'visits' => 22, 
     'newVisits' => 16, 
     'newVisitsPercent' => 72.7)); 
?> 

试图在所有SO结果基于所述键的值排序进行搜索。请让我知道我们需要使用哪个功能。

我看着到ksort,多排序功能

+1

['usort()'](http://php.net/usort)看看相关的列 – 2012-07-18 12:49:32

+0

'usort'或'array_multisort' – Lake 2012-07-18 12:50:19

+0

通过这个问题看起来像几分钟前:)完全相同,但从用户比以前少配置文件点,我不想猜测或判断,但看起来像有多个帐户的人 – haynar 2012-07-18 12:55:06

回答

4

PHP有()称为usort一个内置功能,可以这些类型的数组排序。

您的比较函数可以是这个样子:

function mycmp($a, $b) { 
    return intval($a['visits']) - intval($b['visits']); 
}