[英]How to merge two arrays into one array?
I have two arrays that looks like this: 我有两个看起来像这样的数组:
0 => string '2014-02-14' (length=10)
1 => string '2014-03-14' (length=10)
2 => string '2014-04-14' (length=10)
3 => string '2014-05-14' (length=10)
4 => string '2014-06-16' (length=10)
5 => string '2014-07-14' (length=10)
and the other: 和另外一个:
0 => string '2014-01-30' (length=10)
1 => string '2014-02-27' (length=10)
2 => string '2014-03-31' (length=10)
3 => string '2014-04-30' (length=10)
4 => string '2014-05-29' (length=10)
5 => string '2014-06-30' (length=10)
what i want to do is to merge this two arrays in a specific way so i can use it forward to CSV, so i need to be like this: 我想要做的是以特定的方式合并这两个数组,所以我可以使用它转发到CSV,所以我需要像这样:
0 => string '2014-02-14,2014-02-14'
so it should be firstarray_value
, secondarray_value
for the new array, is there anyway to manipulate this two arrays to be come one array in that for described above? 所以它应该是firstarray_value
,新数组的secondarray_value
,无论如何操纵这两个数组是为了一个数组,如上所述?
Just try with: 试试:
$array1 = array( /* your data */ );
$array2 = array( /* your data */ );
$output = array();
for ($i = 0; $i < count($array1); ++$i) {
$output[] = $array1[$i] . ',' . $array2[$i];
}
To use it with fputcsv
: 要与fputcsv
一起fputcsv
:
for ($i = 0; $i < count($array1); ++$i) {
$output[] = array($array1[$i], $array2[$i]);
}
foreach ($output as $fields) {
fputcsv($fp, $fields);
}
Edit: 编辑:
You can merge those arrays quicker with: 您可以更快地合并这些数组:
$output = array_map(null, $array1, $array2);
and then also: 然后还:
foreach ($output as $fields) {
fputcsv($fp, $fields);
}
$array3 = array();
foreach($array1 as $key=>$val){
$array3[] = $val.",".$array2[$key];
}
print_r($array3);
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.