php array diff

Insoftservice
Insoftservice used Ask the Experts™
on
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
                )
        )

      
)
Comment
Watch Question

Do more with

Expert Office
EXPERT OFFICE® is a registered trademark of EXPERTS EXCHANGE®
Commented:
Have you tried array_merge_recursive()?

<?php

$a1 = 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
         )
);

$a2 = 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
               )
       )
);

$output = array_merge_recursive($a1, $a2);

print_r($output);

?>

Open in new window


Output:
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] => Array
                (
                    [0] => 911789
                    [1] => 911793
                )

            [dnforward] => 495232
            [dndoa] => 1337625000
            [dndoe] => 1337625000
            [emadminid] => muk
            [emtotid] => 5
            [WBRFNBR] => Array
                (
                    [EML] => 911793
                    [DOM] => 911793
                    [MOB] => 911793
                )

        )

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

        )

)

Open in new window

Author

Commented:
no i will check it out and let u know regarding it.
thx for reply

Author

Commented:
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
Bootstrap 4: Exploring New Features

Learn how to use and navigate the new features included in Bootstrap 4, the most popular HTML, CSS, and JavaScript framework for developing responsive, mobile-first websites.

Commented:
Try this:

function array_merge_special($a1, $a2) {
	$diff = array_merge(array_diff_key($a1, $a2), array_diff_key($a2, $a1));
	$rem1 = array_diff_key($a1, $diff);
	$rem2 = array_diff_key($a2, $diff);
	$out = $diff;
	foreach ($rem1 as $k => $v) {
		$out[$k] = array_merge($v, $rem2[$k]);
	}
	return $out;
}

Open in new window


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

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

        )

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

        )

)

Open in new window

Author

Commented:
thx but its php4 and array_diff_key is for php5 , i am searching for its subsitiute

Author

Commented:
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];
                  }

            }

Author

Commented:
will it do as expected.
please do let me know as i am out of my mind currently
Commented:
Try this, then:

function array_merge_special($a1, $a2) {
	$diff = array_merge(array_diff_key4($a1, $a2), array_diff_key4($a2, $a1));
	$rem1 = array_diff_key4($a1, $diff);
	$rem2 = array_diff_key4($a2, $diff);
	$out = $diff;
	foreach ($rem1 as $k => $v) {
		$out[$k] = array_merge($v, $rem2[$k]);
	}
	return $out;
}

function array_diff_key4($a1, $a2) {
	$out = array();
	foreach ($a1 as $k => $v) {
		if (!array_key_exists($k, $a2)) {
			$out[$k] = $v;
		}
	}
	return $out;
}

Open in new window


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

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

        )

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

        )

)

Open in new window

Author

Commented:
thx, did not chk but it was near to resolve my issue

Do more with

Expert Office
Submit tech questions to Ask the Experts™ at any time to receive solutions, advice, and new ideas from leading industry professionals.

Start 7-Day Free Trial