doujiunai2169 2012-09-25 08:06
浏览 15
已采纳

PHP:使用非常数键来排序多维数组

In PHP, I have this kind of array (sorry for the big example, but it's a specific sort) :

[0] => Array
    (
        [title] => 1
        [desc] => 2
    )
[1] => Array
    (
        [title] => 1
        [desc] => 3
    )
[2] => Array
    (
        [title] => 1
        [desc] => 2
        [content] => 10
    )
[3] => Array
    (
        [title] => 1
        [desc] => 2
        [content] => 11
    )
[4] => Array
    (
        [title] => 1
        [desc] => 3
        [content] => 9
    )
[5] => Array
    (
        [title] => 1
        [desc] => 2
        [content] => 10
        [tag] => 'foo'
    )
[6] => Array
    (
        [title] => 1
        [desc] => 2
        [content] => 10
        [tag] => 'bar'
    )

And I want this output :

[0] => Array
    (
        [title] => 1
        [desc] => 2
    )
[1] => Array
    (
        [title] => 1
        [desc] => 2
        [content] => 10
    )
[2] => Array
    (
        [title] => 1
        [desc] => 2
        [content] => 10
        [tag] => 'bar'
    )
[3] => Array
    (
        [title] => 1
        [desc] => 2
        [content] => 10
        [tag] => 'foo'
    )
[4] => Array
    (
        [title] => 1
        [desc] => 2
        [content] => 11
    )
[5] => Array
    (
        [title] => 1
        [desc] => 3
    )
[6] => Array
    (
        [title] => 1
        [desc] => 3
        [content] => 9
    )

And unfortunetly, array_multisort() can't sort arrays with a non-constant number of keys : it just ignore the all array ... Any idea ?

  • 写回答

3条回答 默认 最新

  • dpb42021 2012-09-25 09:17
    关注

    Here is the sort for the sample code :

    function cmp ($a, $b) {
      if ($a['desc'] == $b['desc']) {
        if (!isset($a['content']) && !isset($b['content'])) {
          return 0;
        }
        else if (!isset($a['content'])) {
          return -1;
        }
        else if (!isset($b['content'])) {
          return 1;
        }
        else {
          if ($a['content'] == $b['content']) {
            if (!isset($a['tag'])) {
              return -1;
            }
            else if (!isset($b['tag'])) {
              return 1;
            }
            else {
              if ($a['tag'] == $b['tag']) {
                return 0;
              }
              else if ($a['tag'] < $b['tag']) {
                return -1;
              }
              else {
                return 1;
              }
            }
            return 0;
          }
          else if ($a['content'] < $b['content']) {
            return -1;
          }
          else {
            return 1;
          }
        }
        return 0;
      }
      else if ($a['desc'] < $b['desc']) {
        return -1;
      }
      else {
        return 1;
      }
    }
    usort($array, 'cmp');
    

    Thanks to @Nelson, @therefromhere, and @xdazz to put me on usort() :)

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论
查看更多回答(2条)

报告相同问题?

悬赏问题

  • ¥15 smptlib使用465端口发送邮件失败
  • ¥200 总是报错,能帮助用python实现程序实现高斯正反算吗?有偿
  • ¥15 对于squad数据集的基于bert模型的微调
  • ¥15 为什么我运行这个网络会出现以下报错?CRNN神经网络
  • ¥20 steam下载游戏占用内存
  • ¥15 CST保存项目时失败
  • ¥15 树莓派5怎么用camera module 3啊
  • ¥20 java在应用程序里获取不到扬声器设备
  • ¥15 echarts动画效果的问题,请帮我添加一个动画。不要机器人回答。
  • ¥15 Attention is all you need 的代码运行