3

I have a multidimensional array which looks like below example:

$collection = array(
  0 => array (
      0 => '1002',        //Push to new array1
      1 => '1003'         //Push to new array2
  ),
  1 => array(
      0 => '7',        //Push to new array1
      1 => '7'         //Push to new array2
  ),
  2 => array(
      0 => 'This is my first comment in the box',        //Push to new array1
      1 => '1This is my first comment in the box'        //Push to new array2
  ),
  3 => array(
      0 => '2014-01-01 01:16:05',        //Push to new array1
      1 => '2014-01-01 01:16:05'         //Push to new array2
  )
);

What i want is this:

$collection = array(
    0 => array (               //Pushed array1 result
        0 => '1002',
        1 => '7',
        2 => 'This is my first comment in the box',
        3 => '2014-01-01 01:16:05'
    ),
    1 => array (               //Pushed array2 result
        0 => '1003',
        1 => '7',
        2 => 'This is my second comment in the box',
        3 => '2014-01-01 01:16:05'
    )
);

I don't know how can i say with words, but as you can see i want the result which looks above. I've tried much but i'm lost now.

mickmackusa
  • 43,625
  • 12
  • 83
  • 136
Rahil Wazir
  • 10,007
  • 11
  • 42
  • 64

3 Answers3

4

Using one of the cool new features of PHP 5.5, the array_column() function

$collection = array(
    array_column($collection, 0),
    array_column($collection, 1)
);

EDIT

If you're running a version of PHP less than 5.5, then there's a userland implementation of array_column() here

EDIT #2

For smaller data volumes, but without any PHP version worries, you can also simply transpose the $collection array

$collection = call_user_func_array(
    'array_map',
    array_merge(
        array(NULL),
        $collection
    )
);

See https://eval.in/84609 for example

Mark Baker
  • 209,507
  • 32
  • 346
  • 385
1

Live Demo: https://eval.in/84605
It should not have no php version issue :)
Try This:

$collection = array(
    0 => array (
        0 => '1002',        //Push to new array1
        1 => '1003'         //Push to new array2
    ),
    1 => array(
        0 => '7',        //Push to new array1
        1 => '7'         //Push to new array2
    ),
    2 => array(
        0 => 'This is my first comment in the box',        //Push to new array1
        1 => '1This is my first comment in the box'        //Push to new array2
    ),
    3 => array(
        0 => '2014-01-01 01:16:05',        //Push to new array1
        1 => '2014-01-01 01:16:05'         //Push to new array2
    )
);


$c = count($collection);
$arr = array();
for($i=0;$i< count($collection[0]);$i++){
    $tempArray =array();
    for($j=0;$j< $c; $j++){
        array_push($tempArray,$collection[$j][$i]);
    }
    array_push($arr,$tempArray);
}
$collection = $arr;
print_r($collection);

OUTPUT:

Array
(
    [0] => Array
        (
            [0] => 1002
            [1] => 7
            [2] => This is my first comment in the box
            [3] => 2014-01-01 01:16:05
        )

    [1] => Array
        (
            [0] => 1003
            [1] => 7
            [2] => 1This is my first comment in the box
            [3] => 2014-01-01 01:16:05
        )

)
Awlad Liton
  • 9,366
  • 2
  • 27
  • 53
1

There are many ways to do this depending on your preferences. Here is an example using foreach()

$collection = array(                                                               
   0 => array (                                                                    
      0 => '1002',        //Push to new array1                                     
      1 => '1003'         //Push to new array2                                     
   ),                                                                              
   1 => array(                                                                     
      0 => '7',        //Push to new array1                                        
      1 => '7'         //Push to new array2                                        
   ),                                                                              
   2 => array(                                                                     
      0 => 'This is my first comment in the box',        //Push to new array1      
      1 => 'This is my second comment in the box'        //Push to new array2      
   ),                                                                              
   3 => array(                                                                     
      0 => '2014-01-01 01:16:05',        //Push to new array1                      
      1 => '2014-01-01 01:16:05'         //Push to new array2                      
   )                                                                               
);                                                                                 

$new_collection = array();                                                         

foreach ($collection as $sub_array) {     
   // This assumes $sub_array always has exactly two elements
   for ($i = 0; $i < 2; $i++) {                                                    
      $new_collection[$i][] = $sub_array[$i];                                      
   }                                                                               
}                                                                                  

print_r($new_collection);                                                          

output:

Array
(
    [0] => Array
        (
            [0] => 1002
            [1] => 7
            [2] => This is my first comment in the box
            [3] => 2014-01-01 01:16:05
        )

    [1] => Array
        (
            [0] => 1003
            [1] => 7
            [2] => This is my second comment in the box
            [3] => 2014-01-01 01:16:05
        )
  )
grebneke
  • 4,414
  • 17
  • 24
  • If subarray length changed then what will be there? – Awlad Liton Jan 01 '14 at 14:02
  • @Awlad Liton: If subarray length changes, it will not work. The example assumes subarray has always two elements, as in the data provided in the question. – grebneke Jan 01 '14 at 14:10
  • he did not provide exact data. he provided sample data – Awlad Liton Jan 01 '14 at 14:38
  • Sample or not, we cannot guess in what ways it could change. If the nesting was deeper, what would happen, etc? Anyway, I've added a comment to reflect the requirements on the data. Thank you. – grebneke Jan 01 '14 at 14:44