从每个子数组中收集数组值

时间:2014-01-01 12:57:13

标签: php arrays

我有一个多维数组,如下例所示:

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

我想要的是:

$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'
    )
);

我不知道怎么用文字说,但你可以看到我想要的结果看起来如上。我已经尝试了很多,但现在我迷路了。

3 个答案:

答案 0 :(得分:4)

使用PHP 5.5的一个很酷的新功能,array_column()功能

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

修改

如果您运行的PHP版本低于5.5,则有一个用户态实现array_column()here

编辑#2

对于较小的数据量,但没有任何PHP版本的担忧,您也可以简单地转置$ collection数组

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

例如,请参阅https://eval.in/84609

答案 1 :(得分:1)

现场演示: https://eval.in/84605
它不应该没有PHP版本问题:)
试试这个:

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

<强>输出:

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
        )

)

答案 2 :(得分:1)

根据您的偏好,有很多方法可以做到这一点。以下是使用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);                                                          

输出:

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