Home >Backend Development >PHP Tutorial > php 合龙两个二维数组

php 合龙两个二维数组

WBOYWBOYWBOYWBOYWBOYWBOYWBOYWBOYWBOYWBOYWBOYWBOYWB
WBOYWBOYWBOYWBOYWBOYWBOYWBOYWBOYWBOYWBOYWBOYWBOYWBOriginal
2016-06-13 13:00:27830browse

php 合并两个二维数组
按时间排序的

<br />
<br />
$data1 = array(<br />
<br />
    [0] => array(<br />
        times => '2012-10-10',<br />
        type =>0,<br />
        num =>100<br />
    ),<br />
<br />
    [1] => array(<br />
        times => '2012-10-11',<br />
        type =>0,<br />
        num =>103<br />
    ),<br />
<br />
  ....<br />
       <br />
)<br />
<br />
$data2 = array(<br />
<br />
    [0] => array(<br />
        times => '2012-10-10',<br />
        type =>1,<br />
        num =>400<br />
    ),<br />
<br />
    [1] => array(<br />
        times => '2012-10-11',<br />
        type =>1,<br />
        num =>403<br />
    ),<br />
<br />
  ....<br />
       <br />
)<br />
<br />


两个数组times 是一样的,怎么把它们合并成

<br />
$data = array(<br />
<br />
    [0] => array(<br />
        times => '2012-10-10',<br />
        type =>0,<br />
        num =>100<br />
        type =>1,<br />
        num =>400<br />
    ),<br />
<br />
    [1] => array(<br />
        times => '2012-10-11',<br />
        type =>0,<br />
        num =>103<br />
        type =>1,<br />
        num =>403<br />
    ),<br />
<br />
  ....<br />
       <br />
)<br />


求教

------解决方案--------------------
楼主,就冲你那0的结贴率,呵呵。
<br />
 $date=array(array("time"=>"2012-11-10","type0"=>0,"num0"=>100),array("time"=>"2012-11-11","type0"=>0,"num0"=>103));<br />
 $date1=array(array("time"=>"2012-11-10","type1"=>1,"num1"=>105),array("time"=>"2012-11-11","type1"=>1,"num1"=>120));<br />
 $cnt=count($date);<br />
 for($i=0;$i<$cnt;$i++){<br />
	$date2[$i]=array_merge($date[$i],$date1[$i]);<br />
}<br />
 echo "<pre class="brush:php;toolbar:false">";<br />
 print_r($date);<br />
 print_r($date1);<br />
 print_r($date2);<br />
 echo "
";
//结果如下
Array
(
    [0] => Array
        (
            [time] => 2012-11-10
            [type0] => 0
            [num0] => 100
        )

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