Two number combinations and related issues

Source: Internet
Author: User
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
)

)

)

)
  • Contact Us

    The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

    If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

    A Free Trial That Lets You Build Big!

    Start building with 50+ products and up to 12 months usage for Elastic Compute Service

    • Sales Support

      1 on 1 presale consultation

    • After-Sales Support

      24/7 Technical Support 6 Free Tickets per Quarter Faster Response

    • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.