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,多排序功能

4

2 回答 2

4

PHP 有一个名为usort () 的内置函数,可以对这些类型的数组进行排序。

您的比较函数可能如下所示:

function mycmp($a, $b) {
  return intval($a['visits']) - intval($b['visits']);
}
于 2012-07-18T12:54:08.920 回答
4

看看文档usorthttp ://www.php.net/manual/en/function.usort.php

于 2012-07-18T12:49:15.410 回答