Two array merge issues

Source: Internet
Author: User
Array 1:
$arr 1=array ([0] = = Array ([TIME] = 8:00[detail] = = Array ([0] = = Array ([Caseid] = 31[casename] = Zhang San) [1 ] = = Array ([Caseid] = 32[casename] = Lee IV) [2] = = Array ([Caseid] = 33[casename] = Harry))) [1] = Ar Ray ([TIME] = 12:00[detail] = = Array ([0] = = Array ([Caseid] = 32[casename] = Lee IV) [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] = Lee IV) [2] = = Array ([Caseid] = 33[casename] =&G T 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])))) after merging, the final array should appear like this: [code=php] $result =array ([0] = = Array ([TIME] = 8:00[detail] =&gt ;  Array ([0] = = Array ([Caseid] = 31[casename] = Zhang San) [1] = = Array ([Caseid] (= = 32[casename] = Li iv) [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] = Lee IV) [1] =&gt ; 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] = Lee IV) [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])))

[/code]


Reply to discussion (solution)

$arr 1 = Array (0 = = Array (' time ' = ' 8:00am ', ' detail ' = = Array (0 = = Array (        ' Caseid ' = ' + ', ' casename ' = ' Zhang San ', ' 1 ' = ' Array (' caseid ' = ' 32 ', ' Casename ' = ' John Doe ',), 2 = = Array (' Caseid ' = ' + ', ' casename ' = ' Harry ',) ,),), 1 = = Array (' time ' = = ', ' detail ' = = Array (0 = = Array (' C Aseid ' = ' + ', ' casename ' = ' John Doe ', ' 1 ' = ' array ' (' caseid ' = ' + ', ' CA Sename ' = ' + ' Harry ',),), 2 = = Array (' time ' = ' 18:00 ', ' detail ' = = = Array (0 =&G       T  Array (' caseid ' = ' + ', ' casename ' = ' Zhang San ',), 1 = = Array (' Caseid ' = ' + ', ' casename ' = ' John Doe ',), 2 = = Array (' Caseid ' = ' + ', ' casename ' =&gt ; ' Harry ',),    ),), $arr 2 = Array (0 = = Array (' time ' = ' 8:00am ', ' detail2 ' = = Array (0 = a Rray (' caseid ' = ' + ', ' casename ' and ' Liu Bei ',),), 1 = = Array (' time ' = ' 16:      xx ', ' detail2 ' = = Array (0 = = Array (' Caseid ' = ' ", ' casename ' = ' Zhang Fei '), ), 1 = = Array (' Caseid ' = ' + ', ' casename ' = ' Zhao Yun ',),), 2 = a        Rray (' 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;p rint_r (array_values (array_merge_recursive ($a, $b));
Array ([0] = = Array ([TIME] = = Array ([0] = 8:00  [1] = 8:00am) [detail] = = Array ([0] = = Array ([Caseid] = [Casename] = Zhang  III) [1] = = Array ([Caseid]                        [Casename] = Li iv) [2] = = Array                        ([Caseid] = [Casename] = Harry                        )) [DETAIL2] = = Array ([0] = = Array                        ([Caseid] = [Casename] = Liu Bei          )      )) [1] = = Array ([TIME] = [detail] = = Array                            ([0] = = Array ([Caseid] = 32                            [Casename] = Li iv) [1] = = Array (                [Caseid] = [Casename] = Harry)                     )) [2] = = Array ([TIME] = 18:00 [detail] = = Array (                            [0] = = Array ([Caseid] = 31                            [Casename] = Zhang San) [1] = = Array (                    [Caseid] = [Casename] = Li Si) [2] = = Array                       ([Caseid] = [Casename] = Harry ))) [3] = = Array ([TIME] = 16:00 [Detail 2] = = Array ([0] = = Array ([CA                        Seid] [Casename] = Zhang Fei) [1] = = Array                        [Caseid] [casename] = Zhao Yun ))) [4] = = Array ([TIME] = 22:00 [Detail 2] = = Array ([0] = = Array ([CA Seid] [Casename] ())))

PHP provides a rich range of functions
You should move it yourself.

Array 2 is problematic, 18:00, and 22:00 subscript are 2, change 22:00 to 3

 '; $arr 1 = Array (0 = = Array (' time ' = ' 8:00am ', ' detail ' = = Array (0 = = Array (        ' Caseid ' = ' + ', ' casename ' = ' Zhang San ', ' 1 ' = ' Array (' caseid ' = ' 32 ',      ' Casename ' = ' John Doe ',), 2 = = Array (' Caseid ' + ', ' casename ' = ' Harry ',        ),), 1 = = Array (' time ' = ', ' detail ' = = Array (0 = = Array ( ' Caseid ' = ' + ', ' casename ' = ' John Doe ', ' 1 ' = ' Array (' caseid ' = ' 33 ', ' Casename ' = ' Harry ',),), 2 = = Array (' time ' = ' 18:00 ', ' detail ' = = = Array (0 = > Array (' caseid ' = ' + ', ' casename ' = ' Zhang San ',), 1 = = Array (' CA Seid ' = ' + ', ' casename ' = ' John Doe ', ' 2 ' = ' array ' (' caseid ' = ' + ', ' CAs      Ename ' = ' Harry ',),),), $arr 2 = Array (0 = = Array (' time ' = ' 8:00am ', ' detail2 ' = = = Array (0 = Array (' caseid ' = ' + ', ' casename ' = ' Liu Bei ',),), 1 = = Array (' time ' = ' 1       6:00pm ', ' detail2 ' = = Array (0 = = Array (' Caseid ' = ' ", ' casename ' = ' Zhang Fei '),      ), 1 = = Array (' Caseid ' = ' + ', ' casename ' = ' Zhao Yun ',),), 2 = Array (' time ' = ' 18:00 ', ' detail2 ' = = Array (0 = = Array (' Caseid ' = ' 43 ') , ' casename ' = ' Zhao Yun ',),), 3 = = Array (' time ' = ' 22:00 ', ' detail2 ' = arr Ay (0 = = Array (' Caseid ' + ', ' casename ' = ' marten cicada ',),), $result = arr    Ay (); 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 ' + a    Rray (), ' detail2 ' = $row [' detail2 ']);    }else{$result [$key] [' detail2 '] = $row [' Detail2 ']; }}ksort ($result, sort_numeric); $result = Array_values ($result); Echo '
';p rint_r ($result); Echo '
';? >


Array ([0] = ([TIME] = 8:00am [detail] = = Array (                            [0] = = Array ([Caseid] = 31                            [Casename] = Zhang San) [1] = = Array ( [Caseid] = [Casename] = Li si) [2 ] = = Array ([Caseid] = [Casename] = Harry)) [detail2] = = Array ([0 ] = = Array ([Caseid] = [Casename]            = Liu Bei)) [1] = = Array ([Time] = 12:00 [Detail] = ArRay ([0] = = Array ([Caseid] = =                        [Casename] = Li iv) [1] = = Array                        ([Caseid] = [Casename] = Harry        )) [DETAIL2] = = Array ()) [2] = = Array ([Time] = 16:00 [Detail] = Array () [Detail 2] = = Array ([0] = = Array ([CA                        Seid] [Casename] = Zhang Fei) [1] = = Array                        [Caseid] [casename] = Zhao Yun     )           )) [3] = = Array ([TIME] = 18:00 [detail] = = Array                            ([0] = = Array ([Caseid] = 31                            [Casename] = Zhang San) [1] = = Array (                    [Caseid] = [Casename] = Li Si)                            [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] = [Casenam E] = mink cicada)))

Thank you very much, I took care of myself.

  • 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.