Two array merge issues
This post was last edited by bluezsh on 2014-10-16 16:25:27
Array 1:
$arr 1=array
(
[0] = = Array
(
[Time] = 8:00
[detail] = = Array
(
[0] = = Array
(
[Caseid] = 31
[Casename] = Zhang San
)
[1] = = Array
(
[Caseid] = 32
[Casename] = John Doe
)
[2] = = Array
(
[Caseid] = 33
[Casename] = Harry
)
)
)
[1] = = Array
(
[Time] = 12:00
[detail] = = Array
(
[0] = = Array
(
[Caseid] = 32
)
[1] = = Array
(
[Caseid] = 33
)
)
)
[2] = = Array
(
[Time] = 18:00
[detail] = = Array
(
[0] = = Array
(
[Caseid] = 31
[Casename] = Zhang San
)
[1] = = Array
(
[Caseid] = 32
[Casename] = John Doe
)
[2] = = Array
(
[Caseid] = 33
[Casename] = Harry
)
)
)
)
Array 2
$arr 2=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 Cicada
)
)
)
)
After merging, the final array should be displayed like this:
[code=php]
$result =array
(
[0] = = Array
(
[Time] = 8:00
[detail] = = Array
(
[0] = = Array
(
[Caseid] = 31
[Casename] = Zhang San
)
[1] = = Array
(
[Caseid] = 32
[Casename] = John Doe
)
[2] = = Array
(
[Caseid] = 33
[Casename] = Harry
)
)
[DETAIL2] = = Array
(
[0] = = Array
(
[Caseid] = 41
[Casename] = Liu Bei
)
)
)
[1] = = Array
(
[Time] = 12:00
[detail] = = Array
(
[0] = = Array
(
[Caseid] = 32
[Casename] = John Doe
)
[1] = = Array
(
[Caseid] = 33
[Casename] = Harry
)
)
[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] = Zhang San
)
[1] = = Array
(
[Caseid] = 32
[Casename] = John Doe
)
[2] = = Array
(
[Caseid] = 33
[Casename] = Harry
)
)
[DETAIL2] = = Array
(
[0] = = Array
(
[Caseid] = 43
[Casename] = Zhao Yun
)
)
)
[4] = = Array
(
[Time] = 22:00
[detail] = = Array
(
)
[DETAIL2] = = Array
(
[0] = = Array
(
[Caseid] = 45
[Casename] = Mink Cicada
)
)
)
)
[/code]
------to solve the idea----------------------
$arr 1 = Array (
0 =
Array (
' Time ' = ' 8:00 ',
' Detail ' =
Array (
0 =
Array (
' Caseid ' = ' 31 ',
' Casename ' = ' Zhang San ',
),
1 =
Array (
' Caseid ' = ' 32 ',
' Casename ' = ' John Doe ',
),
2 =
Array (
' Caseid ' = ' 33 ',
' Casename ' = ' Harry ',
),
),
),
1 =
Array (
' Time ' = ' 12:00 ',
' Detail ' =
Array (
0 =
Array (
' Caseid ' = ' 32 ',
' Casename ' = ' John Doe ',
),
1 =
Array (
' Caseid ' = ' 33 ',
' Casename ' = ' Harry ',
),
),
),
2 =
Array (
' Time ' = ' 18:00 ',
' Detail ' =
Array (
0 =
Array (
' Caseid ' = ' 31 ',
' Casename ' = ' Zhang San ',
),
1 =
Array (
' Caseid ' = ' 32 ',
' Casename ' = ' John Doe ',
),
2 =
Array (
' Caseid ' = ' 33 ',
' Casename ' = ' Harry ',
),
),
),
);
$arr 2 = 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 ' = ' marten cicada ',
),
),
),
);
foreach ($arr 1 as $item) $a [$item [' time ']] = $item;
foreach ($arr 2 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] = Zhang San
)
[1] = = Array
(
[Caseid] = 32
[Casename] = John Doe
)
[2] = = Array
(
[Caseid] = 33
[Casename] = Harry
)
)
[DETAIL2] = = Array
(
[0] = = Array
(
[Caseid] = 41
[Casename] = Liu Bei
)
)
)
[1] = = Array
(
[Time] = 12:00
[detail] = = Array
(
[0] = = Array
(
[Caseid] = 32
[Casename] = John Doe
)
[1] = = Array
(
[Caseid] = 33
[Casename] = Harry
)
)
)
[2] = = Array
(
[Time] = 18:00
[detail] = = Array
(
[0] = = Array
(
[Caseid] = 31
[Casename] = Zhang San
)
[1] = = Array
(
[Caseid] = 32
[Casename] = John Doe
)
[2] = = Array
(
[Caseid] = 33
[Casename] = Harry
)
)
)
[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 Cicada
)
)
)
)
PHP provides a rich range of functions
You should move it yourself.
------to solve the idea----------------------
Array 2 is problematic, 18:00, and 22:00 subscript are 2, change 22:00 to 3
Echo '
';
$arr 1 = Array (
0 =
Array (
' Time ' = ' 8:00 ',
' Detail ' =
Array (
0 =
Array (
' Caseid ' = ' 31 ',
' Casename ' = ' Zhang San ',
),
1 =
Array (
' Caseid ' = ' 32 ',
' Casename ' = ' John Doe ',
),
2 =
Array (
' Caseid ' = ' 33 ',
' Casename ' = ' Harry ',
),
),
),
1 =
Array (
' Time ' = ' 12:00 ',
' Detail ' =
Array (
0 =
Array (
' Caseid ' = ' 32 ',
' Casename ' = ' John Doe ',
),
1 =
Array (
' Caseid ' = ' 33 ',
' Casename ' = ' Harry ',
),
),
),
2 =
Array (
' Time ' = ' 18:00 ',
' Detail ' =
Array (
0 =
Array (
' Caseid ' = ' 31 ',
' Casename ' = ' Zhang San ',
),
1 =
Array (
' Caseid ' = ' 32 ',
' Casename ' = ' John Doe ',
),
2 =
Array (
' Caseid ' = ' 33 ',
' Casename ' = ' Harry ',
),
),
),
);
$arr 2 = 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 ' = ' marten cicada ',
),
),
),
);
$result = Array ();
foreach ($arr 1 as $row) {
$key = $row [' Time '];
$row [' detail2 '] = array ();
$result [$key] = $row;
}
foreach ($arr 2 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] = Zhang San
)
[1] = = Array
(
[Caseid] = 32
[Casename] = John Doe
)
[2] = = Array
(
[Caseid] = 33
[Casename] = Harry
)
)
[DETAIL2] = = Array
(
[0] = = Array
(
[Caseid] = 41
[Casename] = Liu Bei
)
)
)
[1] = = Array
(
[Time] = 12:00
[detail] = = Array
(
[0] = = Array
(
[Caseid] = 32
[Casename] = John Doe
)
[1] = = Array
(
[Caseid] = 33
[Casename] = Harry
)
)
[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] = Zhang San
)
[1] = = Array
(
[Caseid] = 32
[Casename] = John Doe
)
[2] = = Array
(
[Caseid] = 33
[Casename] = Harry
)
)
[DETAIL2] = = Array
(
[0] = = Array
(
[Caseid] = 43
[Casename] = Zhao Yun
)
)
)
[4] = = Array
(
[Time] = 22:00
[detail] = = Array
(
)
[DETAIL2] = = Array
(
[0] = = Array
(
[Caseid] = 45
[Casename] = Mink Cicada
)
)
)
)