Avatar of Insoftservice
Insoftservice
Flag for India asked on

php array diff

hi,
i have two array on coming from db and other from xml.

i have to merge it keeping in mind the order and its data.
as per order 'Array 1' has to be kept in higher order, but if its present in 'Array 2' then it's order has to be maintain accordingly and data has to be merge with 'Array 2' over here its for 'muk'

Array 1
Array
(
    [vinay] => Array
        (
            [payment_date] => 0
            [dnname] => vinay
            [dnprivate] =>
            [wbcrstmps] => 911789
            [dnforward] => 495232
            [dndoa] => 1337625000
            [dndoe] => 1337625000
        )

    [muk] => Array
        (
            [payment_date] => 0
            [dnname] => muk
            [dnprivate] =>
            [wbcrstmps] => 911789
            [dnforward] => 495232
            [dndoa] => 1337625000
            [dndoe] => 1337625000
        )
)
Array 2
Array
(
    [dxvdf.in] => Array
        (
            [emadminid] => no_email
            [wbcrstmps] => 911793
            [emtotid] => 5
            [WBRFNBR] => Array
                (
                    [EML] => 911793
                    [DOM] => 911793
                    [MOB] => 911793
                )
      )

      [muk] => Array
        (
            [emadminid] => muk
            [wbcrstmps] => 911793
            [emtotid] => 5
            [WBRFNBR] => Array
                (
                    [EML] => 911793
                    [DOM] => 911793
                    [MOB] => 911793
                )
      )
)


o/p

Array
(
    [dxvdf.in] => Array
        (
            [emadminid] => no_email
            [wbcrstmps] => 911793
            [emtotid] => 5
            [WBRFNBR] => Array
                (
                    [EML] => 911793
                    [DOM] => 911793
                    [MOB] => 911793
                )
      )

    [vinay] => Array
        (
            [payment_date] => 0
            [dnname] => vinay
            [dnprivate] =>
            [wbcrstmps] => 911789
            [dnforward] => 495232
            [dndoa] => 1337625000
            [dndoe] => 1337625000
        )

    [muk] => Array
        (
            [payment_date] => 0
            [dnname] => muk
            [dnprivate] =>
            [wbcrstmps] => 911789
            [dnforward] => 495232
            [dndoa] => 1337625000
            [dndoe] => 1337625000
          [emadminid] => muk
            [emtotid] => 5
            [WBRFNBR] => Array
                (
                    [EML] => 911793
                    [DOM] => 911793
                    [MOB] => 911793
                )
        )

      
)
PHP

Avatar of undefined
Last Comment
Insoftservice

8/22/2022 - Mon
SOLUTION
StingRaY

Log in or sign up to see answer
Become an EE member today7-DAY FREE TRIAL
Members can start a 7-Day Free trial then enjoy unlimited access to the platform
Sign up - Free for 7 days
or
Learn why we charge membership fees
We get it - no one likes a content blocker. Take one extra minute and find out why we block content.
Not exactly the question you had in mind?
Sign up for an EE membership and get your own personalized solution. With an EE membership, you can ask unlimited troubleshooting, research, or opinion questions.
ask a question
Insoftservice

ASKER
no i will check it out and let u know regarding it.
thx for reply
Insoftservice

ASKER
no,
it did not worked, as the o/p gives the data ay array 1 to for the common key (over here muk).

whereas muk value from array2 has to remain and the additional value has to be appended in it
SOLUTION
StingRaY

Log in or sign up to see answer
Become an EE member today7-DAY FREE TRIAL
Members can start a 7-Day Free trial then enjoy unlimited access to the platform
Sign up - Free for 7 days
or
Learn why we charge membership fees
We get it - no one likes a content blocker. Take one extra minute and find out why we block content.
Not exactly the question you had in mind?
Sign up for an EE membership and get your own personalized solution. With an EE membership, you can ask unlimited troubleshooting, research, or opinion questions.
ask a question
Insoftservice

ASKER
thx but its php4 and array_diff_key is for php5 , i am searching for its subsitiute
This is the best money I have ever spent. I cannot not tell you how many times these folks have saved my bacon. I learn so much from the contributors.
rwheeler23
Insoftservice

ASKER
i did it in such fashion

$count = count($array2);
            $i=1;
            foreach($array1 as $dom => $domval)
            {
            // echo $dom;exit;
             $exist = array_key_exists($dom,$array2);
             if($exist)
             {
              $xmldom[] = $dom;
              // $details[$dom] = $array2[$dom];
             }
             else
             {
               $i++;
               $details[$dom] = $array1[$dom];
             }
            
            }
            
            if($i < $count)
            {
                    foreach($array2 as $domxml => $domxmlval)
                  {
                  $details[$domxml] = $array2[$domxml];
                  }

            }
Insoftservice

ASKER
will it do as expected.
please do let me know as i am out of my mind currently
ASKER CERTIFIED SOLUTION
Log in to continue reading
Log In
Sign up - Free for 7 days
Get an unlimited membership to EE for less than $4 a week.
Unlimited question asking, solutions, articles and more.
Insoftservice

ASKER
thx, did not chk but it was near to resolve my issue
Get an unlimited membership to EE for less than $4 a week.
Unlimited question asking, solutions, articles and more.