首頁 > 後端開發 > php教程 > 两个数组合并有关问题

两个数组合并有关问题

WBOY
發布: 2016-06-13 12:08:38
原創
802 人瀏覽過

两个数组合并问题

本帖最后由 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 />
登入後複製
相關標籤:
來源:php.cn
本網站聲明
本文內容由網友自願投稿,版權歸原作者所有。本站不承擔相應的法律責任。如發現涉嫌抄襲或侵權的內容,請聯絡admin@php.cn
熱門教學
更多>
最新下載
更多>
網站特效
網站源碼
網站素材
前端模板