两个数组合并有关问题

WBOY
Libérer: 2016-06-13 12:08:38
original
782 Les gens l'ont consulté

两个数组合并问题

本帖最后由 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 />
Copier après la connexion

数组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 />
Copier après la connexion

[/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 />
Copier après la connexion
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 />
Copier après la connexion

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 '
Copier après la connexion
';

?>


<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 />
Copier après la connexion
Étiquettes associées:
source:php.cn
Déclaration de ce site Web
Le contenu de cet article est volontairement contribué par les internautes et les droits d'auteur appartiennent à l'auteur original. Ce site n'assume aucune responsabilité légale correspondante. Si vous trouvez un contenu suspecté de plagiat ou de contrefaçon, veuillez contacter admin@php.cn
Tutoriels populaires
Plus>
Derniers téléchargements
Plus>
effets Web
Code source du site Web
Matériel du site Web
Modèle frontal
À propos de nous Clause de non-responsabilité Sitemap
Site Web PHP chinois:Formation PHP en ligne sur le bien-être public,Aidez les apprenants PHP à grandir rapidement!