ホームページ  >  記事  >  バックエンド開発  >  2 つの配列のマージの問題

2 つの配列のマージの問題

WBOY
WBOYオリジナル
2016-06-23 13:46:53795ブラウズ

配列 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 に変更します。

声明:
この記事の内容はネチズンが自主的に寄稿したものであり、著作権は原著者に帰属します。このサイトは、それに相当する法的責任を負いません。盗作または侵害の疑いのあるコンテンツを見つけた場合は、admin@php.cn までご連絡ください。