Home >Backend Development >PHP Tutorial >php 怎么把下列两个数组合并到一个数组里边去

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

WBOY
WBOYOriginal
2016-06-23 14:00:09946browse

第一个数组如:


第二个数组:


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



请教高手,怎么实现,谢谢!!


回复讨论(解决方案)

$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这四位大侠,你们的代码都能运行出我想要的效果,谢谢各位了。分就给大家平分了!!再次感谢你们!!!

Statement:
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn