2013-07-13 246 views
0

我有這個多維數組 我想知道我怎麼能再次排序這個數組,所以我可以在for循環中使用它。php多維數組排序

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但沒有任何工程! 請大家幫忙,謝謝大家:)

+0

你需要使用'array_multisort' – DevZer0

+0

這是什麼意思 - 排序?你只需要改變數組的兩個維的鍵 –

回答

0

使用array_multisort排序多維數組或使用多個鍵排序數組。

+0

我的數組是多個鍵,我怎樣才能使用(array_multisort)函數來做到這一點?! –

0

我認爲這應該做到這一點,但如果你沒有額外的關卡,它會更加清潔。

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

非常感謝Andy Gee。它的工作很好:) –

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