两个数组合并问题

WBOY
풀어 주다: 2016-06-23 13:46:53
원래의
784명이 탐색했습니다.

数组1:

$arr1=Array		(			[0] => Array				(					[time] => 8:00					[detail] => Array						(							[0] => Array								(									[caseid] => 31									[casename] => 张三									 								)							[1] => Array								(									[caseid] => 32									[casename] =>李四									 								)							[2] => Array								(									[caseid] => 33									[casename] => 王五									 								)		 						)		 				)		 			[1] => Array				(					[time] => 12:00					[detail] => Array						(							[0] => Array								(									[caseid] => 32									[casename] => 李四 								)							[1] => Array								(									[caseid] => 33									[casename] => 王五 								)		 						)		 				)		 			[2] => Array				(					[time] => 18:00					[detail] => Array						(							[0] => Array								(									[caseid] => 31									[casename] => 张三									 								)							[1] => Array								(									[caseid] => 32									[casename] => 李四									 								)							[2] => Array								(									[caseid] => 33									[casename] => 王五									 								)		 						)		 				)		 		)
로그인 후 복사

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

[/code]


回复讨论(解决方案)

$arr1 = array (  0 =>   array (    'time' => '8:00',    'detail' =>     array (      0 =>       array (        'caseid' => '31',        'casename' => '张三',      ),      1 =>       array (        'caseid' => '32',        'casename' => '李四',      ),      2 =>       array (        'caseid' => '33',        'casename' => '王五',      ),    ),  ),  1 =>   array (    'time' => '12:00',    'detail' =>     array (      0 =>       array (        'caseid' => '32',        'casename' => '李四',      ),      1 =>       array (        'caseid' => '33',        'casename' => '王五',      ),    ),  ),  2 =>   array (    'time' => '18:00',    'detail' =>     array (      0 =>       array (        'caseid' => '31',        'casename' => '张三',      ),      1 =>       array (        'caseid' => '32',        'casename' => '李四',      ),      2 =>       array (        'caseid' => '33',        'casename' => '王五',      ),    ),  ),);$arr2 = array (  0 =>   array (    'time' => '8:00',    'detail2' =>     array (      0 =>       array (        'caseid' => '41',        'casename' => '刘备',      ),    ),  ),  1 =>   array (    'time' => '16:00',    'detail2' =>     array (      0 =>       array (        'caseid' => '42',        'casename' => '张飞',      ),      1 =>       array (        'caseid' => '43',        'casename' => '赵云',      ),    ),  ),  2 =>   array (    'time' => '22:00',    'detail2' =>     array (      0 =>       array (        'caseid' => '45',        'casename' => '貂蝉',      ),    ),  ),);foreach($arr1 as $item) $a[$item['time']] = $item;foreach($arr2 as $item) $b[$item['time']] = $item;print_r(array_values(array_merge_recursive($a, $b)));
로그인 후 복사
Array(    [0] => Array        (            [time] => Array                (                    [0] => 8:00                    [1] => 8:00                )            [detail] => Array                (                    [0] => Array                        (                            [caseid] => 31                            [casename] => 张三                        )                    [1] => Array                        (                            [caseid] => 32                            [casename] => 李四                        )                    [2] => Array                        (                            [caseid] => 33                            [casename] => 王五                        )                )            [detail2] => Array                (                    [0] => Array                        (                            [caseid] => 41                            [casename] => 刘备                        )                )        )    [1] => Array        (            [time] => 12:00            [detail] => Array                (                    [0] => Array                        (                            [caseid] => 32                            [casename] => 李四                        )                    [1] => Array                        (                            [caseid] => 33                            [casename] => 王五                        )                )        )    [2] => Array        (            [time] => 18:00            [detail] => Array                (                    [0] => Array                        (                            [caseid] => 31                            [casename] => 张三                        )                    [1] => Array                        (                            [caseid] => 32                            [casename] => 李四                        )                    [2] => Array                        (                            [caseid] => 33                            [casename] => 王五                        )                )        )    [3] => Array        (            [time] => 16:00            [detail2] => Array                (                    [0] => Array                        (                            [caseid] => 42                            [casename] => 张飞                        )                    [1] => Array                        (                            [caseid] => 43                            [casename] => 赵云                        )                )        )    [4] => Array        (            [time] => 22:00            [detail2] => Array                (                    [0] => Array                        (                            [caseid] => 45                            [casename] => 貂蝉                        )                )        ))
로그인 후 복사

php 提供了丰富的函数
你应该自己动动手

数组2有问题,18:00,与 22:00的下标都是2,把22:00改为3

<?phpecho '<meta http-equiv="content-type" content="text/html;charset=utf-8">';$arr1 = array (  0 =>   array (    'time' => '8:00',    'detail' =>     array (      0 =>       array (        'caseid' => '31',        'casename' => '张三',      ),      1 =>       array (        'caseid' => '32',        'casename' => '李四',      ),      2 =>       array (        'caseid' => '33',        'casename' => '王五',      ),    ),  ),  1 =>   array (    'time' => '12:00',    'detail' =>     array (      0 =>       array (        'caseid' => '32',        'casename' => '李四',      ),      1 =>       array (        'caseid' => '33',        'casename' => '王五',      ),    ),  ),  2 =>   array (    'time' => '18:00',    'detail' =>     array (      0 =>       array (        'caseid' => '31',        'casename' => '张三',      ),      1 =>       array (        'caseid' => '32',        'casename' => '李四',      ),      2 =>       array (        'caseid' => '33',        'casename' => '王五',      ),    ),  ),);$arr2 = array (  0 =>   array (    'time' => '8:00',    'detail2' =>     array (      0 =>       array (        'caseid' => '41',        'casename' => '刘备',      ),    ),  ),  1 =>   array (    'time' => '16:00',    'detail2' =>     array (      0 =>       array (        'caseid' => '42',        'casename' => '张飞',      ),      1 =>       array (        'caseid' => '43',        'casename' => '赵云',      ),    ),  ),    2 =>     array (    'time' => '18:00',    'detail2' =>     array (      0 =>       array (        'caseid' => '43',        'casename' => '赵云',      ),    ),    ),  3 =>   array (    'time' => '22:00',    'detail2' =>     array (      0 =>       array (        'caseid' => '45',        'casename' => '貂蝉',      ),    ),  ),);$result = array();foreach($arr1 as $row){    $key = $row['time'];    $row['detail2'] = array();    $result[$key] = $row;}foreach($arr2 as $row){    $key = $row['time'];    if(!isset($result[$key])){        $result[$key] = array(            'time' => $row['time'],            'detail' => array(),            'detail2' => $row['detail2']        );    }else{       $result[$key]['detail2'] = $row['detail2'];    }}ksort($result, SORT_NUMERIC);$result = array_values($result);echo '<pre class="brush:php;toolbar:false">';print_r($result);echo '
로그인 후 복사
';?>

Array(    [0] => Array        (            [time] => 8:00            [detail] => Array                (                    [0] => Array                        (                            [caseid] => 31                            [casename] => 张三                        )                    [1] => Array                        (                            [caseid] => 32                            [casename] => 李四                        )                    [2] => Array                        (                            [caseid] => 33                            [casename] => 王五                        )                )            [detail2] => Array                (                    [0] => Array                        (                            [caseid] => 41                            [casename] => 刘备                        )                )        )    [1] => Array        (            [time] => 12:00            [detail] => Array                (                    [0] => Array                        (                            [caseid] => 32                            [casename] => 李四                        )                    [1] => Array                        (                            [caseid] => 33                            [casename] => 王五                        )                )            [detail2] => Array                (                )        )    [2] => Array        (            [time] => 16:00            [detail] => Array                (                )            [detail2] => Array                (                    [0] => Array                        (                            [caseid] => 42                            [casename] => 张飞                        )                    [1] => Array                        (                            [caseid] => 43                            [casename] => 赵云                        )                )        )    [3] => Array        (            [time] => 18:00            [detail] => Array                (                    [0] => Array                        (                            [caseid] => 31                            [casename] => 张三                        )                    [1] => Array                        (                            [caseid] => 32                            [casename] => 李四                        )                    [2] => Array                        (                            [caseid] => 33                            [casename] => 王五                        )                )            [detail2] => Array                (                    [0] => Array                        (                            [caseid] => 43                            [casename] => 赵云                        )                )        )    [4] => Array        (            [time] => 22:00            [detail] => Array                (                )            [detail2] => Array                (                    [0] => Array                        (                            [caseid] => 45                            [casename] => 貂蝉                        )                )        ))
로그인 후 복사

非常感谢,自己搞定了。

관련 라벨:
원천:php.cn
본 웹사이트의 성명
본 글의 내용은 네티즌들의 자발적인 기여로 작성되었으며, 저작권은 원저작자에게 있습니다. 본 사이트는 이에 상응하는 법적 책임을 지지 않습니다. 표절이나 침해가 의심되는 콘텐츠를 발견한 경우 admin@php.cn으로 문의하세요.
최신 이슈
인기 튜토리얼
더>
최신 다운로드
더>
웹 효과
웹사이트 소스 코드
웹사이트 자료
프론트엔드 템플릿
회사 소개 부인 성명 Sitemap
PHP 중국어 웹사이트:공공복지 온라인 PHP 교육,PHP 학습자의 빠른 성장을 도와주세요!