Rumah  >  Artikel  >  pembangunan bahagian belakang  >  两个数组合并有关问题

两个数组合并有关问题

WBOY
WBOYasal
2016-06-13 12:08:38780semak imbas

两个数组合并问题

本帖最后由 bluezsh 于 2014-10-16 16:25:27 编辑 数组1:
<br />$arr1=Array<br />		(<br />			[0] => Array<br />				(<br />					[time] => 8:00<br />					[detail] => Array<br />						(<br />							[0] => Array<br />								(<br />									[caseid] => 31<br />									[casename] => 张三<br />									 <br />								)<br />							[1] => Array<br />								(<br />									[caseid] => 32<br />									[casename] =>李四<br />									 <br />								)<br />							[2] => Array<br />								(<br />									[caseid] => 33<br />									[casename] => 王五<br />									 <br />								)<br />		 <br />						)<br />		 <br />				)<br />		 <br />			[1] => Array<br />				(<br />					[time] => 12:00<br />					[detail] => Array<br />						(<br />							[0] => Array<br />								(<br />									[caseid] => 32<br />									[casename] => 李四 <br />								)<br />							[1] => Array<br />								(<br />									[caseid] => 33<br />									[casename] => 王五 <br />								)<br />		 <br />						)<br />		 <br />				)<br />		 <br />			[2] => Array<br />				(<br />					[time] => 18:00<br />					[detail] => Array<br />						(<br />							[0] => Array<br />								(<br />									[caseid] => 31<br />									[casename] => 张三<br />									 <br />								)<br />							[1] => Array<br />								(<br />									[caseid] => 32<br />									[casename] => 李四<br />									 <br />								)<br />							[2] => Array<br />								(<br />									[caseid] => 33<br />									[casename] => 王五<br />									 <br />								)<br />		 <br />						)<br />		 <br />				)<br />		 <br />		)<br /><br />

数组2
<br />$arr2=Array<br />		(<br />			[0] => Array<br />				(<br />					[time] => 8:00<br />					[detail2] => Array<br />						(<br />							[0] => Array<br />								(<br />									[caseid] => 41<br />									[casename] => 刘备<br />									 <br />								)<br />							<br />						)<br />		 <br />				)<br />		 <br />			[1] => Array<br />				(<br />					[time] => 16:00<br />					[detail2] => Array<br />						(<br />							[0] => Array<br />								(<br />									[caseid] => 42<br />									[casename] => 张飞 <br />								)<br />							[1] => Array<br />								(<br />									[caseid] => 43<br />									[casename] => 赵云 <br />								)<br />		 <br />						)<br />		 <br />				)<br />		 <br />			[2] => Array<br />				(<br />					[time] => 18:00<br />					[detail2] => Array<br />						(<br />							[0] => Array<br />								(<br />									[caseid] => 43<br />									[casename] => 赵云<br />									 <br />								)<br />		 <br />						)<br />		 <br />				)<br />			[2] => Array<br />				(<br />					[time] => 22:00<br />					[detail2] => Array<br />						(<br />							[0] => Array<br />								(<br />									[caseid] => 45<br />									[casename] => 貂蝉<br />									 <br />								)<br />		 <br />						)<br />		 <br />				)<br />		 <br />		)<br /><br />合并后,最终数组应该是这样显示:<br />[code=php]<br />$result=Array<br />		(<br />			[0] => Array<br />				(<br />					[time] => 8:00<br />					[detail] => Array<br />						(<br />							[0] => Array<br />								(<br />									[caseid] => 31<br />									[casename] => 张三<br />									 <br />								)<br />							[1] => Array<br />								(<br />									[caseid] => 32<br />									[casename] =>李四<br />									 <br />								)<br />							[2] => Array<br />								(<br />									[caseid] => 33<br />									[casename] => 王五<br />									 <br />								)<br />		 <br />						)<br />						[detail2] => Array<br />						(<br />							[0] => Array<br />								(<br />									[caseid] => 41<br />									[casename] => 刘备<br />									 <br />								)<br />							<br />						)<br />		 <br />				)<br />		 <br />			[1] => Array<br />				(<br />					[time] => 12:00<br />					[detail] => Array<br />						(<br />							[0] => Array<br />								(<br />									[caseid] => 32<br />									[casename] => 李四 <br />								)<br />							[1] => Array<br />								(<br />									[caseid] => 33<br />									[casename] => 王五 <br />								)<br />		 <br />						)<br />					[detail2] => Array<br />						(<br /><br />						)<br />		 <br />				)<br />			[2] => Array<br />				(<br />					[time] => 16:00<br />					[detail] => Array<br />						(<br /><br />						)<br />					[detail2] => Array<br />						(<br />							[0] => Array<br />								(<br />									[caseid] => 42<br />									[casename] => 张飞 <br />								)<br />							[1] => Array<br />								(<br />									[caseid] => 43<br />									[casename] => 赵云 <br />								)<br />		 <br />						)<br />		 <br />				)<br /><br />		 <br />			[3] => Array<br />				(<br />					[time] => 18:00<br />					[detail] => Array<br />						(<br />							[0] => Array<br />								(<br />									[caseid] => 31<br />									[casename] => 张三<br />									 <br />								)<br />							[1] => Array<br />								(<br />									[caseid] => 32<br />									[casename] => 李四<br />									 <br />								)<br />							[2] => Array<br />								(<br />									[caseid] => 33<br />									[casename] => 王五<br />									 <br />								)<br />		 <br />						)<br />					[detail2] => Array<br />						(<br />							[0] => Array<br />								(<br />									[caseid] => 43<br />									[casename] => 赵云<br />									 <br />								)<br />		 <br />						)<br />		 <br />				)<br />			[4] => Array<br />				(<br />					[time] => 22:00<br />					[detail] => Array<br />						(<br /><br />						)<br />					[detail2] => Array<br />						(<br />							[0] => Array<br />								(<br />									[caseid] => 45<br />									[casename] => 貂蝉<br />									 <br />								)<br />		 <br />						)<br />		 <br />				)<br />		 <br />		)<br />

[/code]
------解决思路----------------------
$arr1 = array (<br />  0 => <br />  array (<br />    'time' => '8:00',<br />    'detail' => <br />    array (<br />      0 => <br />      array (<br />        'caseid' => '31',<br />        'casename' => '张三',<br />      ),<br />      1 => <br />      array (<br />        'caseid' => '32',<br />        'casename' => '李四',<br />      ),<br />      2 => <br />      array (<br />        'caseid' => '33',<br />        'casename' => '王五',<br />      ),<br />    ),<br />  ),<br />  1 => <br />  array (<br />    'time' => '12:00',<br />    'detail' => <br />    array (<br />      0 => <br />      array (<br />        'caseid' => '32',<br />        'casename' => '李四',<br />      ),<br />      1 => <br />      array (<br />        'caseid' => '33',<br />        'casename' => '王五',<br />      ),<br />    ),<br />  ),<br />  2 => <br />  array (<br />    'time' => '18:00',<br />    'detail' => <br />    array (<br />      0 => <br />      array (<br />        'caseid' => '31',<br />        'casename' => '张三',<br />      ),<br />      1 => <br />      array (<br />        'caseid' => '32',<br />        'casename' => '李四',<br />      ),<br />      2 => <br />      array (<br />        'caseid' => '33',<br />        'casename' => '王五',<br />      ),<br />    ),<br />  ),<br />);<br />$arr2 = array (<br />  0 => <br />  array (<br />    'time' => '8:00',<br />    'detail2' => <br />    array (<br />      0 => <br />      array (<br />        'caseid' => '41',<br />        'casename' => '刘备',<br />      ),<br />    ),<br />  ),<br />  1 => <br />  array (<br />    'time' => '16:00',<br />    'detail2' => <br />    array (<br />      0 => <br />      array (<br />        'caseid' => '42',<br />        'casename' => '张飞',<br />      ),<br />      1 => <br />      array (<br />        'caseid' => '43',<br />        'casename' => '赵云',<br />      ),<br />    ),<br />  ),<br />  2 => <br />  array (<br />    'time' => '22:00',<br />    'detail2' => <br />    array (<br />      0 => <br />      array (<br />        'caseid' => '45',<br />        'casename' => '貂蝉',<br />      ),<br />    ),<br />  ),<br />);<br />foreach($arr1 as $item) $a[$item['time']] = $item;<br />foreach($arr2 as $item) $b[$item['time']] = $item;<br />print_r(array_values(array_merge_recursive($a, $b)));<br />
Array<br />(<br />    [0] => Array<br />        (<br />            [time] => Array<br />                (<br />                    [0] => 8:00<br />                    [1] => 8:00<br />                )<br /><br />            [detail] => Array<br />                (<br />                    [0] => Array<br />                        (<br />                            [caseid] => 31<br />                            [casename] => 张三<br />                        )<br /><br />                    [1] => Array<br />                        (<br />                            [caseid] => 32<br />                            [casename] => 李四<br />                        )<br /><br />                    [2] => Array<br />                        (<br />                            [caseid] => 33<br />                            [casename] => 王五<br />                        )<br /><br />                )<br /><br />            [detail2] => Array<br />                (<br />                    [0] => Array<br />                        (<br />                            [caseid] => 41<br />                            [casename] => 刘备<br />                        )<br /><br />                )<br /><br />        )<br /><br />    [1] => Array<br />        (<br />            [time] => 12:00<br />            [detail] => Array<br />                (<br />                    [0] => Array<br />                        (<br />                            [caseid] => 32<br />                            [casename] => 李四<br />                        )<br /><br />                    [1] => Array<br />                        (<br />                            [caseid] => 33<br />                            [casename] => 王五<br />                        )<br /><br />                )<br /><br />        )<br /><br />    [2] => Array<br />        (<br />            [time] => 18:00<br />            [detail] => Array<br />                (<br />                    [0] => Array<br />                        (<br />                            [caseid] => 31<br />                            [casename] => 张三<br />                        )<br /><br />                    [1] => Array<br />                        (<br />                            [caseid] => 32<br />                            [casename] => 李四<br />                        )<br /><br />                    [2] => Array<br />                        (<br />                            [caseid] => 33<br />                            [casename] => 王五<br />                        )<br /><br />                )<br /><br />        )<br /><br />    [3] => Array<br />        (<br />            [time] => 16:00<br />            [detail2] => Array<br />                (<br />                    [0] => Array<br />                        (<br />                            [caseid] => 42<br />                            [casename] => 张飞<br />                        )<br /><br />                    [1] => Array<br />                        (<br />                            [caseid] => 43<br />                            [casename] => 赵云<br />                        )<br /><br />                )<br /><br />        )<br /><br />    [4] => Array<br />        (<br />            [time] => 22:00<br />            [detail2] => Array<br />                (<br />                    [0] => Array<br />                        (<br />                            [caseid] => 45<br />                            [casename] => 貂蝉<br />                        )<br /><br />                )<br /><br />        )<br /><br />)<br /><br />

php 提供了丰富的函数
你应该自己动动手
------解决思路----------------------
数组2有问题,18:00,与 22:00的下标都是2,把22:00改为3
<br /><?php<br />echo '<meta http-equiv="content-type" content="text/html;charset=utf-8">';<br />$arr1 = array (<br />  0 => <br />  array (<br />    'time' => '8:00',<br />    'detail' => <br />    array (<br />      0 => <br />      array (<br />        'caseid' => '31',<br />        'casename' => '张三',<br />      ),<br />      1 => <br />      array (<br />        'caseid' => '32',<br />        'casename' => '李四',<br />      ),<br />      2 => <br />      array (<br />        'caseid' => '33',<br />        'casename' => '王五',<br />      ),<br />    ),<br />  ),<br />  1 => <br />  array (<br />    'time' => '12:00',<br />    'detail' => <br />    array (<br />      0 => <br />      array (<br />        'caseid' => '32',<br />        'casename' => '李四',<br />      ),<br />      1 => <br />      array (<br />        'caseid' => '33',<br />        'casename' => '王五',<br />      ),<br />    ),<br />  ),<br />  2 => <br />  array (<br />    'time' => '18:00',<br />    'detail' => <br />    array (<br />      0 => <br />      array (<br />        'caseid' => '31',<br />        'casename' => '张三',<br />      ),<br />      1 => <br />      array (<br />        'caseid' => '32',<br />        'casename' => '李四',<br />      ),<br />      2 => <br />      array (<br />        'caseid' => '33',<br />        'casename' => '王五',<br />      ),<br />    ),<br />  ),<br />);<br />$arr2 = array (<br />  0 => <br />  array (<br />    'time' => '8:00',<br />    'detail2' => <br />    array (<br />      0 => <br />      array (<br />        'caseid' => '41',<br />        'casename' => '刘备',<br />      ),<br />    ),<br />  ),<br />  1 => <br />  array (<br />    'time' => '16:00',<br />    'detail2' => <br />    array (<br />      0 => <br />      array (<br />        'caseid' => '42',<br />        'casename' => '张飞',<br />      ),<br />      1 => <br />      array (<br />        'caseid' => '43',<br />        'casename' => '赵云',<br />      ),<br />    ),<br />  ),<br />    2 => <br />    array (<br />    'time' => '18:00',<br />    'detail2' => <br />    array (<br />      0 => <br />      array (<br />        'caseid' => '43',<br />        'casename' => '赵云',<br />      ),<br />    ),<br />    ),<br />  3 => <br />  array (<br />    'time' => '22:00',<br />    'detail2' => <br />    array (<br />      0 => <br />      array (<br />        'caseid' => '45',<br />        'casename' => '貂蝉',<br />      ),<br />    ),<br />  ),<br />);<br /><br />$result = array();<br /><br />foreach($arr1 as $row){<br />    $key = $row['time'];<br />    $row['detail2'] = array();<br />    $result[$key] = $row;<br />}<br /><br />foreach($arr2 as $row){<br />    $key = $row['time'];<br />    if(!isset($result[$key])){<br />        $result[$key] = array(<br />            'time' => $row['time'],<br />            'detail' => array(),<br />            'detail2' => $row['detail2']<br />        );<br />    }else{<br />       $result[$key]['detail2'] = $row['detail2'];<br />    }<br />}<br /><br />ksort($result, SORT_NUMERIC);<br /><br />$result = array_values($result);<br /><br />echo '<pre class="brush:php;toolbar:false">';<br />print_r($result);<br />echo '
';

?>


<br />Array<br />(<br />    [0] => Array<br />        (<br />            [time] => 8:00<br />            [detail] => Array<br />                (<br />                    [0] => Array<br />                        (<br />                            [caseid] => 31<br />                            [casename] => 张三<br />                        )<br /><br />                    [1] => Array<br />                        (<br />                            [caseid] => 32<br />                            [casename] => 李四<br />                        )<br /><br />                    [2] => Array<br />                        (<br />                            [caseid] => 33<br />                            [casename] => 王五<br />                        )<br /><br />                )<br /><br />            [detail2] => Array<br />                (<br />                    [0] => Array<br />                        (<br />                            [caseid] => 41<br />                            [casename] => 刘备<br />                        )<br /><br />                )<br /><br />        )<br /><br />    [1] => Array<br />        (<br />            [time] => 12:00<br />            [detail] => Array<br />                (<br />                    [0] => Array<br />                        (<br />                            [caseid] => 32<br />                            [casename] => 李四<br />                        )<br /><br />                    [1] => Array<br />                        (<br />                            [caseid] => 33<br />                            [casename] => 王五<br />                        )<br /><br />                )<br /><br />            [detail2] => Array<br />                (<br />                )<br /><br />        )<br /><br />    [2] => Array<br />        (<br />            [time] => 16:00<br />            [detail] => Array<br />                (<br />                )<br /><br />            [detail2] => Array<br />                (<br />                    [0] => Array<br />                        (<br />                            [caseid] => 42<br />                            [casename] => 张飞<br />                        )<br /><br />                    [1] => Array<br />                        (<br />                            [caseid] => 43<br />                            [casename] => 赵云<br />                        )<br /><br />                )<br /><br />        )<br /><br />    [3] => Array<br />        (<br />            [time] => 18:00<br />            [detail] => Array<br />                (<br />                    [0] => Array<br />                        (<br />                            [caseid] => 31<br />                            [casename] => 张三<br />                        )<br /><br />                    [1] => Array<br />                        (<br />                            [caseid] => 32<br />                            [casename] => 李四<br />                        )<br /><br />                    [2] => Array<br />                        (<br />                            [caseid] => 33<br />                            [casename] => 王五<br />                        )<br /><br />                )<br /><br />            [detail2] => Array<br />                (<br />                    [0] => Array<br />                        (<br />                            [caseid] => 43<br />                            [casename] => 赵云<br />                        )<br /><br />                )<br /><br />        )<br /><br />    [4] => Array<br />        (<br />            [time] => 22:00<br />            [detail] => Array<br />                (<br />                )<br /><br />            [detail2] => Array<br />                (<br />                    [0] => Array<br />                        (<br />                            [caseid] => 45<br />                            [casename] => 貂蝉<br />                        )<br /><br />                )<br /><br />        )<br /><br />)<br />
Kenyataan:
Kandungan artikel ini disumbangkan secara sukarela oleh netizen, dan hak cipta adalah milik pengarang asal. Laman web ini tidak memikul tanggungjawab undang-undang yang sepadan. Jika anda menemui sebarang kandungan yang disyaki plagiarisme atau pelanggaran, sila hubungi admin@php.cn