简体   繁体   中英

php multidimensional array sort

I have this multidimensional array I am wondered how can i sort this array again so i can use it in for loop.

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)

How can I reindex this array to become

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)

I tried array_shift and array_chunk but nothing works !!! Please help, thank you all :)

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

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

You can use 'array_values' for re-indexing which start index from 0. As per your requirement inner array are not starting from 0 but are same as parent array index. You have to use foreach for that. To index the way you want can be done like this:

$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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM