Php 从每个子数组收集数组值
我有一个多维数组,如下例所示:Php 从每个子数组收集数组值,php,arrays,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'
$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'
)
);
我不知道我该如何用语言表达,但正如你所看到的,我想要的结果是高瞻远瞩的。我已经尝试了很多,但是现在我迷路了。使用PHP5.5的一个很酷的新特性,函数 编辑 如果您运行的PHP版本低于5.5,那么就有一个array_column()的userland实现 编辑#2 对于较小的数据量,但不必担心任何PHP版本,您也可以简单地转置$collection数组
$collection = call_user_func_array(
'array_map',
array_merge(
array(NULL),
$collection
)
);
例如,请参见现场演示:
它不应该有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);
根据您的喜好,有很多方法可以做到这一点。下面是一个使用
foreach()
它给出了第一个arrayIt应该给出所需的输出或一个错误(如果PHP<5.5),它当然不应该给出原始arrayI已经安装了PHP 5.3.5版本我检查了5.5.1版本的代码。如果您安装的版本是PHP 5.3.5,那么你需要链接的userland实现来使用这个函数。让我们看看,如果子数组长度发生了变化,那么会出现什么情况?@Awlad Liton:如果子数组长度发生变化,它将无法工作。该示例假设子阵列始终有两个元素,如问题中提供的数据。他没有提供确切的数据。他是否提供了样本数据样本,我们无法猜测它会以何种方式发生变化。如果筑巢更深,会发生什么情况,等等?无论如何,我添加了一条注释以反映对数据的要求。非常感谢。
$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
)
)
$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
)
)