doremifasodo0008008 2017-03-27 00:13
浏览 13
已采纳

PHP按几个标准排序数组

I am coding with PHP and having a multidimensional array. I would like to sort my array. The most important criteria is total points. The second criteria is name.

In other words, the array should be sorted that way:

1st by [info][total], descending

2nd by [info][name], ascending

Array (
  [183] => Array (
    [3] => Array (
      [1] => Array ()
    )

    [1] => Array (
      [8] => Array ()
    )

    [info] => Array (
      [name] => Doe John /* 2nd criteria */
      [total] => 4800 /* 1st criteria */
    )
  )

  [380] => Array (
    [4] => Array (
      [6] => Array ()
    )

    [info] => Array (
      [name] => Davis Ben /* 2nd criteria */
      [total] => 14500 /* 1st criteria */
    )

    [2] => Array (
      [5] => Array ()
    )
  )
)

So, the result should be like this:

Array (
  [380] => Array (
    [4] => Array (
      [6] => Array ()
    )

    [info] => Array (
      [name] => Davis Ben
      [total] => 14500
    )

    [2] => Array (
      [5] => Array ()
    )
  )

  [183] => Array (
    [3] => Array (
      [1] => Array ()
    )

    [1] => Array (
      [8] => Array ()
    )

    [info] => Array (
      [name] => Doe John
      [total] => 4800
    )
  )
)

I have tried this:

foreach ($array as &$item)
{
  uasort($item, function ($a, $b)
  {
    if ($a['total'] == $b['total']
    {
      return $b['name'] - $a['name'];
    }
    else
    {
      return $b['total'] - $a['total'];
    }
  }); 
}

I also tried this, but it does not help:

foreach ($array as &$item)
{
  uasort($item, function ($a, $b)
  {
    if ($a['info']['total'] == $b['info']['total'])
    {
      return $b['info']['name'] - $a['info']['name'];
    }
    else
    {
      return $b['info']['total'] - $a['info']['total'];
    }
  });
}
  • 写回答

1条回答 默认 最新

  • duanpanyang1962 2017-03-27 11:23
    关注

    The request of your question (order the values by custom criteria, keep the associations between keys and values) perfectly matches the description of PHP function uasort().

    The code is something like this:

    $input = Array(
        '183' => Array(
            '3' => Array(
                '1' => Array(),
            ),
            '1' => Array(
                '8' => Array(),
            ),
            'info' => Array(
                'name'  => 'Doe John',      /* 2nd criteria */
                'total' => 4800,            /* 1st criteria */
            ),
        ),
        '380' => Array(
            '4' => Array(
                '6' => Array(),
            ),
            'info' => Array(
                'name'  => 'Davis Ben',     /* 2nd criteria */
                'total' => 14500,           /* 1st criteria */
            ),
            '2' => Array(
                '5' => Array(),
            ),
        ),
    );
    
    uasort($input, function (array $a, array $b) {
        // compare using the 1st criterion
        if ($a['info']['total'] != $b['info']['total']) {
            return $b['info']['total'] - $a['info']['total'];       // < 0 when $a > $b (descending)
        }
    
        // equality on the 1st criterion, use the 2nd one
        return strcmp($a['info']['name'], $b['info']['name']);      // < 0 when $a < $b (ascending)
    });
    
    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?

悬赏问题

  • ¥15 (标签-UDP|关键词-client)
  • ¥15 关于库卡officelite无法与虚拟机通讯的问题
  • ¥15 qgcomp混合物线性模型分析的代码出现错误:Model aliasing occurred
  • ¥100 已有python代码,要求做成可执行程序,程序设计内容不多
  • ¥15 目标检测项目无法读取视频
  • ¥15 GEO datasets中基因芯片数据仅仅提供了normalized signal如何进行差异分析
  • ¥15 小红薯封设备能解决的来
  • ¥100 求采集电商背景音乐的方法
  • ¥15 数学建模竞赛求指导帮助
  • ¥15 STM32控制MAX7219问题求解答