1,">
当前位置:Gxlcms > PHP教程 > php如何把下列两个数组合并到一个数组里边去?

php如何把下列两个数组合并到一个数组里边去?

时间:2021-07-01 10:21:17 帮助过:4人阅读

php 怎么把下列两个数组合并到一个数组里边去??
第一个数组如:


第二个数组:


两个数组合并到一个数组的效果如下:



请教高手,怎么实现,谢谢!!
------解决方案--------------------
$arr1 = array("0"=>1,'1'=>20,'2'=>37,'3'=>43);
$arr2 = array('0'=>array('times'=>'2014-02-25','counts'=>'19'),'1'=>array('times'=>'2014-02-26','counts'=>'17'),'2'=>array('times'=>'2014-02-27','counts'=>'6'),'3'=>array('times'=>'2014-02-28','counts'=>'8'));
$arr = array_intersect_key($arr2,$arr1);
foreach ($arr2 as $key => $value) {
$arr2[$key]['incnum'] = $arr1[$key];
unset($arr2[$key]['counts']);
}
var_dump($arr2);

------解决方案--------------------

$a = array(1,20,37,43);
$b = array(array('times'=>'2014-02-25','counts'=>'19'),array('times'=>'2014-02-26','counts'=>'17'),array('times'=>'2014-02-27','counts'=>'6'),array('times'=>'2014-02-28','counts'=>'8'));
$c = array();
for($i=0,$len=count($a); $i<$len; $i++){
array_push($c, array('times'=>$b[$i]['times'],'incnum'=>$a[$i]));
}
var_dump($c);
?>

------解决方案--------------------
$a = array(1,20,37,43);
$b = array(array('times'=>'2014-02-25','counts'=>'19'),array('times'=>'2014-02-26','counts'=>'17'),array('times'=>'2014-02-27','counts'=>'6'),array('times'=>'2014-02-28','counts'=>'8'));
$c = array_map(function($a, $b) { return array('times'=>$b['times'],'incnum'=>$a); }, $a, $b);
var_dump($c);

------解决方案--------------------
$arr1 = array("0"=>1,'1'=>20,'2'=>37,'3'=>43);
$arr2 = array(
'0'=>array('times'=>'2014-02-25','counts'=>'19'),
'1'=>array('times'=>'2014-02-26','counts'=>'17'),
'2'=>array('times'=>'2014-02-27','counts'=>'6'),
'3'=>array('times'=>'2014-02-28','counts'=>'8')
);
$arr3=array();
function my($arr1,$arr2,$arr3){
$i=0;
foreach($arr2 as $k=>$v){
foreach($v as $k_k =>$v_v){
if($k_k=='times'){
$arr3[$i]['date_time']=$v_v;
$arr3[$i]['incnum']=$arr1[$i];
$i++;
}
}
}
return $arr3;
}
print_r(my($arr1,$arr2,$arr3));

人气教程排行