Merge the two arrays. in this post, we edit the array 1: $ arr1Array ([0] & nbsp; & gt; & nbsp; from bluezsh to 2014-10-1616: 25: 27; array ([time] & nbsp; & gt; & nbsp; 8:00 [detail] & nbsp; & gt
This post was last edited by bluezsh at 16:25:27
Array 1:
$ Arr1 = Array
(
[0] => Array
(
[Time] => 8: 00
[Detail] => Array
(
[0] => Array
(
[Caseid] => 31
[Casename] => James
)
[1] => Array
(
[Caseid] => 32
[Casename] => Li Si
)
[2] => Array
(
[Caseid] => 33
[Casename] => Wang Wu
)
)
)
[1] => Array
(
[Time] => 12: 00
[Detail] => Array
(
[0] => Array
(
[Caseid] => 32
[Casename] => Li Si
)
[1] => Array
(
[Caseid] => 33
[Casename] => Wang Wu
)
)
)
[2] => Array
(
[Time] => 18: 00
[Detail] => Array
(
[0] => Array
(
[Caseid] => 31
[Casename] => James
)
[1] => Array
(
[Caseid] => 32
[Casename] => Li Si
)
[2] => Array
(
[Caseid] => 33
[Casename] => Wang Wu
)
)
)
)
Array 2
$ Arr2 = Array
(
[0] => Array
(
[Time] => 8: 00
[Detail2] => Array
(
[0] => Array
(
[Caseid] => 41
[Casename] => Liu Bei
)
)
)
[1] => Array
(
[Time] => 16: 00
[Detail2] => Array
(
[0] => Array
(
[Caseid] => 42
[Casename] => Zhang Fei
)
[1] => Array
(
[Caseid] => 43
[Casename] => Zhao Yun
)
)
)
[2] => Array
(
[Time] => 18: 00
[Detail2] => Array
(
[0] => Array
(
[Caseid] => 43
[Casename] => Zhao Yun
)
)
)
[2] => Array
(
[Time] => 22: 00
[Detail2] => Array
(
[0] => Array
(
[Caseid] => 45
[Casename] => mink Chan
)
)
)
)
After merging, the final array should be displayed as follows:
[Code = php]
$ Result = Array
(
[0] => Array
(
[Time] => 8: 00
[Detail] => Array
(
[0] => Array
(
[Caseid] => 31
[Casename] => James
)
[1] => Array
(
[Caseid] => 32
[Casename] => Li Si
)
[2] => Array
(
[Caseid] => 33
[Casename] => Wang Wu
)
)
[Detail2] => Array
(
[0] => Array
(
[Caseid] => 41
[Casename] => Liu Bei
)
)
)
[1] => Array
(
[Time] => 12: 00
[Detail] => Array
(
[0] => Array
(
[Caseid] => 32
[Casename] => Li Si
)
[1] => Array
(
[Caseid] => 33
[Casename] => Wang Wu
)
)
[Detail2] => Array
(
)
)
[2] => Array
(
[Time] => 16: 00
[Detail] => Array
(
)
[Detail2] => Array
(
[0] => Array
(
[Caseid] => 42
[Casename] => Zhang Fei
)
[1] => Array
(
[Caseid] => 43
[Casename] => Zhao Yun
)
)
)
[3] => Array
(
[Time] => 18: 00
[Detail] => Array
(
[0] => Array
(
[Caseid] => 31
[Casename] => James
)
[1] => Array
(
[Caseid] => 32
[Casename] => Li Si
)
[2] => Array
(
[Caseid] => 33
[Casename] => Wang Wu
)
)
[Detail2] => Array
(
[0] => Array
(
[Caseid] => 43
[Casename] => Zhao Yun
)
)
)
[4] => Array
(
[Time] => 22: 00
[Detail] => Array
(
)
[Detail2] => Array
(
[0] => Array
(
[Caseid] => 45
[Casename] => mink Chan
)
)
)
)
[/Code]
------ Solution ----------------------
$ Arr1 = array (
0 =>
Array (
'Time' => '8: 00 ',
'Detail' =>
Array (
0 =>
Array (
'Caseid' => '31 ',
'Casename' => 'Zhang San ',
),
1 =>
Array (
'Caseid' => '32 ',
'Casename' => 'Li Si ',
),
2 =>
Array (
'Caseid' => '33 ',
'Casename' => 'Wang 5 ',
),
),
),
1 =>
Array (
'Time' => '12: 00 ',
'Detail' =>
Array (
0 =>
Array (
'Caseid' => '32 ',
'Casename' => 'Li Si ',
),
1 =>
Array (
'Caseid' => '33 ',
'Casename' => 'Wang 5 ',
),
),
),
2 =>
Array (
'Time' => '18: 00 ',
'Detail' =>
Array (
0 =>
Array (
'Caseid' => '31 ',
'Casename' => 'Zhang San ',
),
1 =>
Array (
'Caseid' => '32 ',
'Casename' => 'Li Si ',
),
2 =>
Array (
'Caseid' => '33 ',
'Casename' => 'Wang 5 ',
),
),
),
);
$ Arr2 = array (
0 =>
Array (
'Time' => '8: 00 ',
'Detail2' =>
Array (
0 =>
Array (
'Caseid' => '41 ',
'Casename' => 'Liu Bei ',
),
),
),
1 =>
Array (
'Time' => '16: 00 ',
'Detail2' =>
Array (
0 =>
Array (
'Caseid' => '42 ',
'Casename' => 'Zhang Fei ',
),
1 =>
Array (
'Caseid' => '43 ',
'Casename' => 'Zhao Yun ',
),
),
),
2 =>
Array (
'Time' => '22: 00 ',
'Detail2' =>
Array (
0 =>
Array (
'Caseid' => '45 ',
'Casename' => 'marker ',
),
),
),
);
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] => James
)
[1] => Array
(
[Caseid] => 32
[Casename] => Li Si
)
[2] => Array
(
[Caseid] => 33
[Casename] => Wang Wu
)
)
[Detail2] => Array
(
[0] => Array
(
[Caseid] => 41
[Casename] => Liu Bei
)
)
)
[1] => Array
(
[Time] => 12: 00
[Detail] => Array
(
[0] => Array
(
[Caseid] => 32
[Casename] => Li Si
)
[1] => Array
(
[Caseid] => 33
[Casename] => Wang Wu
)
)
)
[2] => Array
(
[Time] => 18: 00
[Detail] => Array
(
[0] => Array
(
[Caseid] => 31
[Casename] => James
)
[1] => Array
(
[Caseid] => 32
[Casename] => Li Si
)
[2] => Array
(
[Caseid] => 33
[Casename] => Wang Wu
)
)
)
[3] => Array
(
[Time] => 16: 00
[Detail2] => Array
(
[0] => Array
(
[Caseid] => 42
[Casename] => Zhang Fei
)
[1] => Array
(
[Caseid] => 43
[Casename] => Zhao Yun
)
)
)
[4] => Array
(
[Time] => 22: 00
[Detail2] => Array
(
[0] => Array
(
[Caseid] => 45
[Casename] => mink Chan
)
)
)
)
Php provides rich functions
You should do it yourself
------ Solution ----------------------
There is a problem with array 2. the subscript at and are both 2. change to 3.
Echo'
';
$ Arr1 = array (
0 =>
Array (
'Time' => '8: 00 ',
'Detail' =>
Array (
0 =>
Array (
'Caseid' => '31 ',
'Casename' => 'Zhang San ',
),
1 =>
Array (
'Caseid' => '32 ',
'Casename' => 'Li Si ',
),
2 =>
Array (
'Caseid' => '33 ',
'Casename' => 'Wang 5 ',
),
),
),
1 =>
Array (
'Time' => '12: 00 ',
'Detail' =>
Array (
0 =>
Array (
'Caseid' => '32 ',
'Casename' => 'Li Si ',
),
1 =>
Array (
'Caseid' => '33 ',
'Casename' => 'Wang 5 ',
),
),
),
2 =>
Array (
'Time' => '18: 00 ',
'Detail' =>
Array (
0 =>
Array (
'Caseid' => '31 ',
'Casename' => 'Zhang San ',
),
1 =>
Array (
'Caseid' => '32 ',
'Casename' => 'Li Si ',
),
2 =>
Array (
'Caseid' => '33 ',
'Casename' => 'Wang 5 ',
),
),
),
);
$ Arr2 = array (
0 =>
Array (
'Time' => '8: 00 ',
'Detail2' =>
Array (
0 =>
Array (
'Caseid' => '41 ',
'Casename' => 'Liu Bei ',
),
),
),
1 =>
Array (
'Time' => '16: 00 ',
'Detail2' =>
Array (
0 =>
Array (
'Caseid' => '42 ',
'Casename' => 'Zhang Fei ',
),
1 =>
Array (
'Caseid' => '43 ',
'Casename' => 'Zhao Yun ',
),
),
),
2 =>
Array (
'Time' => '18: 00 ',
'Detail2' =>
Array (
0 =>
Array (
'Caseid' => '43 ',
'Casename' => 'Zhao Yun ',
),
),
),
3 =>
Array (
'Time' => '22: 00 ',
'Detail2' =>
Array (
0 =>
Array (
'Caseid' => '45 ',
'Casename' => 'marker ',
),
),
),
);
$ 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'';
print_r($result);
echo '
';
?>
Array
(
[0] => Array
(
[Time] => 8: 00
[Detail] => Array
(
[0] => Array
(
[Caseid] => 31
[Casename] => James
)
[1] => Array
(
[Caseid] => 32
[Casename] => Li Si
)
[2] => Array
(
[Caseid] => 33
[Casename] => Wang Wu
)
)
[Detail2] => Array
(
[0] => Array
(
[Caseid] => 41
[Casename] => Liu Bei
)
)
)
[1] => Array
(
[Time] => 12: 00
[Detail] => Array
(
[0] => Array
(
[Caseid] => 32
[Casename] => Li Si
)
[1] => Array
(
[Caseid] => 33
[Casename] => Wang Wu
)
)
[Detail2] => Array
(
)
)
[2] => Array
(
[Time] => 16: 00
[Detail] => Array
(
)
[Detail2] => Array
(
[0] => Array
(
[Caseid] => 42
[Casename] => Zhang Fei
)
[1] => Array
(
[Caseid] => 43
[Casename] => Zhao Yun
)
)
)
[3] => Array
(
[Time] => 18: 00
[Detail] => Array
(
[0] => Array
(
[Caseid] => 31
[Casename] => James
)
[1] => Array
(
[Caseid] => 32
[Casename] => Li Si
)
[2] => Array
(
[Caseid] => 33
[Casename] => Wang Wu
)
)
[Detail2] => Array
(
[0] => Array
(
[Caseid] => 43
[Casename] => Zhao Yun
)
)
)
[4] => Array
(
[Time] => 22: 00
[Detail] => Array
(
)
[Detail2] => Array
(
[0] => Array
(
[Caseid] => 45
[Casename] => mink Chan
)
)
)
)