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 ?

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() :)

It's not clear to me exactly what criteria you want to order by, but you can use usort, you provide it a function that defines the comparison.

You can use usort with a tailored cmp function for your special needs, like the following code:

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;
}

that should do what you want.