Tell me more ×
Stack Overflow is a question and answer site for professional and enthusiast programmers. It's 100% free, no registration required.

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 :)

share|improve this question
you need to use array_multisort – DevZer0 Jul 13 at 13:10
What does it means - sort? You only need to change keys in 2 dimensions of array – u_mulder Jul 13 at 13:10

3 Answers

Use array_multisort to sort multi-dimensional arrays or to sort an array using multiple keys.

share|improve this answer
my array is multiple key, how can i use (array_multisort) function to do this ?! – Wajdi Jurry Jul 13 at 13:22

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++;
}
share|improve this answer
Thank you so much Andy Gee. It work's fine :) – Wajdi Jurry Jul 13 at 13:42

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
share|improve this answer

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.