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 请问paddlehub能支持移动端开发吗?在Android studio上该如何部署?
  • ¥170 如图所示配置eNSP
  • ¥20 docker里部署springboot项目,访问不到扬声器
  • ¥15 netty整合springboot之后自动重连失效
  • ¥15 悬赏!微信开发者工具报错,求帮改
  • ¥20 wireshark抓不到vlan
  • ¥20 关于#stm32#的问题:需要指导自动酸碱滴定仪的原理图程序代码及仿真
  • ¥20 设计一款异域新娘的视频相亲软件需要哪些技术支持
  • ¥15 stata安慰剂检验作图但是真实值不出现在图上
  • ¥15 c程序不知道为什么得不到结果