Rumah > Artikel > pembangunan bahagian belakang > 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);
<?php$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));
谢谢:microlab2009,fdipzone,xuzuning 版主,luo19880415这四位大侠,你们的代码都能运行出我想要的效果,谢谢各位了。分就给大家平分了!!再次感谢你们!!!