1

在 PHP 中,我有这种数组(对不起,这个大例子,但它是一个特定的排序):

[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'
    )

我想要这个输出:

[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
    )

不幸的是,array_multisort()不能对具有非常量键的数组进行排序:它只是忽略了所有数组......知道吗?

4

3 回答 3

2

我不清楚您要订购的确切标准,但您可以使用usort,为它提供一个定义比较的函数。

于 2012-09-25T08:09:23.780 回答
2

您可以使用为您的特殊需求量usort身定制的cmp功能,如以下代码:

usort($arr, "cmp");

function cmp($a, $b) {
    $array = array('title','desc','content','tag');
    foreach ($array as &$ar) {
        $ai = isset($a[$ar]);
        $bi = isset($b[$ar]);
        if ($ai && !$bi) {
            return 1;
        }
        if ($bi && !$ai) {
            return -1;
        }
        if (!$bi) {
            return 0;
        }
        if ($a[$ar] > $b[$ar]) {
            return 1;
        }
        if ($b[$ar] > $a[$ar]) {
            return -1;
        }
    }
    return 0;
}

那应该做你想做的事。

于 2012-09-25T08:50:04.920 回答
0

这是示例代码的排序:

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');

感谢@Nelson、@therefromhere 和 @xdazz让我上场usort():)

于 2012-09-25T09:17:41.363 回答