php多维数组排序

时间:2013-07-13 13:07:29

标签: php arrays multidimensional-array

我有这个多维数组 我想知道如何再次对这个数组进行排序,以便我可以在for循环中使用它。

array (size=3)
  0 => 
    array (size=1)
      0 => 
        array (size=7)
          'username' => string 'wajdi' (length=5)
          'userimage' => string 'file_3898.jpg' (length=13)
          'date' => int 1373721708
          'postid' => string '118' (length=3)
          'type' => string 'comment' (length=7)
          'comment' => string 'a' (length=1)
          'notify' => string '0' (length=1)
  2 => 
    array (size=1)
      2 => 
        array (size=7)
          'username' => string 'wajdi' (length=5)
          'userimage' => string 'file_3898.jpg' (length=13)
          'date' => int 1373721711
          'postid' => string '118' (length=3)
          'type' => string 'comment' (length=7)
          'comment' => string 'c' (length=1)
          'notify' => string '0' (length=1)
  3 => 
    array (size=1)
      3 => 
        array (size=7)
          'username' => string 'wajdi' (length=5)
          'userimage' => string 'file_3898.jpg' (length=13)
          'date' => int 1373721712
          'postid' => string '118' (length=3)
          'type' => string 'comment' (length=7)
          'comment' => string 'd' (length=1)
          'notify' => string '0' (length=1)

如何重新索引此数组以成为

array (size=3)
  0 => 
    array (size=1)
      0 => 
        array (size=7)
          'username' => string 'wajdi' (length=5)
          'userimage' => string 'file_3898.jpg' (length=13)
          'date' => int 1373721708
          'postid' => string '118' (length=3)
          'type' => string 'comment' (length=7)
          'comment' => string 'a' (length=1)
          'notify' => string '0' (length=1)
  1 => 
    array (size=1)
      1 => 
        array (size=7)
          'username' => string 'wajdi' (length=5)
          'userimage' => string 'file_3898.jpg' (length=13)
          'date' => int 1373721711
          'postid' => string '118' (length=3)
          'type' => string 'comment' (length=7)
          'comment' => string 'c' (length=1)
          'notify' => string '0' (length=1)
  2 => 
    array (size=1)
      2 => 
        array (size=7)
          'username' => string 'wajdi' (length=5)
          'userimage' => string 'file_3898.jpg' (length=13)
          'date' => int 1373721712
          'postid' => string '118' (length=3)
          'type' => string 'comment' (length=7)
          'comment' => string 'd' (length=1)
          'notify' => string '0' (length=1)

我尝试过array_shift和array_chunk但没有任何效果! 请帮忙,谢谢大家:)

3 个答案:

答案 0 :(得分:0)

使用array_multisort对多维数组进行排序或使用多个键对数组进行排序。

答案 1 :(得分:0)

我认为应该这样做,但是如果你没有从阵列中获得额外的水平,它会更加清洁。

$new_array = array();
$index = 0;
foreach($array as $i1 => $a1){
    foreach($a1 as $i2 => $a2){
        $new_array[$index][$index] = $a2;
    }
    $index++;
}

答案 2 :(得分:0)

您可以使用'array_values'重新索引从0开始的索引。根据您的要求,内部数组不是从0开始,而是与父数组索引相同。您必须使用foreach。索引你想要的方式可以像这样:

$info = array(
    0 => array (
        0 => array (
            'username' => 'wajdi',
            'userimage' => 'file_3898.jpg',
            'date' => 1373721708,
            'postid' => '118',
            'type' => 'comment',
            'comment' => 'a',
            'notify' => '0'
        )
    ),
    2 => array (
        2 => array (
            'username' => 'wajdi',
            'userimage' => 'file_3898.jpg',
            'date' => 1373721708,
            'postid' => '118',
            'type' => 'comment',
            'comment' => 'a',
            'notify' => '0'
        )
    ),
    3 => array (
        3 => array (
            'username' => 'wajdi',
            'userimage' => 'file_3898.jpg',
            'date' => 1373721708,
            'postid' => '118',
            'type' => 'comment',
            'comment' => 'a',
            'notify' => '0'
        )
    )
);

var_dump($info); // original index
$info = array_values($info);
foreach ($info as $key => $value) {
    $temp = array();
    foreach($value as $k => $v) {
        $temp[$key] = $v;
    }
    $info[$key] = $temp;
}
var_dump($info); // re-index