Question

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.

Was it helpful?

Solution

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

OTHER TIPS

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
        )

)

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
        )
  )
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top