0

我有这个多维数组 我想知道如何再次对该数组进行排序,以便可以在 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 但没有任何效果!请大家帮忙,谢谢大家:)

4

3 回答 3

0

I think this should do it but it's be a lot cleaner if you didn't have the extra level off the array.

$new_array = array();
$index = 0;
foreach($array as $i1 => $a1){
    foreach($a1 as $i2 => $a2){
        $new_array[$index][$index] = $a2;
    }
    $index++;
}
于 2013-07-13T13:28:52.840 回答
0

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

于 2013-07-13T13:11:18.740 回答
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
于 2013-07-13T14:07:21.500 回答