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.

0

3 Answers 3

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

Sign up to request clarification or add additional context in comments.

7 Comments

Its giving the first array
It should give either the required output or an error (if PHP < 5.5), it certainly shouldn't give the original array
I've installed PHP 5.3.5 version I checked the code at eval.in version 5.5.1.
If your installed version is PHP 5.3.5, then you'll need the linked userland implementation to use this function
|
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
        )

)

Comments

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

4 Comments

If subarray length changed then what will be there?
@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.
he did not provide exact data. he provided sample data
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.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.