简体   繁体   English

PHP多维数组排序

[英]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. 我有这个多维数组,我想知道如何再次对该数组排序,以便可以在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)

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 !!! 我尝试了array_shift和array_chunk但没有任何效果! 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. 您可以使用'array_values'重新索引,索引从0开始。根据您的要求,内部数组不是从0开始,而是与父数组索引相同。 You have to use foreach for that. 您必须为此使用foreach 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

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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