Assume that I have two multidimensional arrays as follows:
Multidimensional array 1 :
$newArray = Array
(
[0] => Array
(
[0] => 45521
[1] => Clothing
[2] => Percent discount
[3] => Get 15% off With Code ABCD15
[4] => 2020-05-18
[5] => 2020-05-31
[6] => ABCD15
[7] => https://domain[dot]com/AYuANDEqU40
[8] => Boutique
[9] => Germany
)
[1] => Array
(
[0] => 45516
[1] => Apparel
[2] => Percentage Off
[3] => 15% off With Code ABCD15
[4] => 2020-05-18
[5] => 2020-05-31
[6] => ABCD15
[7] => https://domain[dot]com/AYuANDEqU40
[8] => Boutique
[9] => Australia
)
[2] => Array
(
[0] => 45672
[1] => Food & Drink
[2] => Free Shipping
[3] => Summer Collection Now. Shop Now!
[4] => 2020-05-17
[5] => 2020-08-01
[6] => N/A
[7] => https://domain[dot]com/AYuANDEqU40761312
[8] => Fancy
[9] => US
)
[3] => Array
(
[0] => 45866
[1] => Food & Drink
[2] => Percentage off
[3] => 20% Off
[4] => 2020-05-17
[5] => 2026-05-15
[6] => OFF20
[7] => https://domain[dot]com/AYuANDEqU40780908
[8] => Biata
[9] => US
)
[4] => Array
(
[0] => 45524
[1] => Health & Wellness
[2] => Other
[3] => Jet's monthly bottle of probiotics. Try it now!
[4] => 2020-05-02
[5] => 2026-05-01
[6] => N/A
[7] => https://domain[dot]com/AYuANDEqU40758409
[8] => Jet
[9] => US
)
)
[5] => Array
(
[0] => 45524
[1] => Health & Wellness
[2] => Other
[3] => Jet seasonal probiotics. No hidden cost.
[4] => 2020-05-02
[5] => 2026-05-01
[6] => N/A
[7] => https://domain[dot]com/AYuANDEqU40758408
[8] => Jet
[9] => US
)
Multidimension array 2 :
$oldArray = Array
(
[0] => Array
(
[0] => 45521
[1] => Clothing's
[2] => Percent discount
[3] => Get 15% off With Code ABCD15
[4] => 2020-05-18
[5] => 2020-05-31
[6] => ABCD15
[7] => https://domain[dot]com/AYuANDEqU40
[8] => Boutique
[9] => Germany
)
[1] => Array
(
[0] => 45516
[1] => Apparel
[2] => Percentage Off
[3] => 15% off With Code ABCD15
[4] => 2020-05-18
[5] => 2020-05-31
[6] => ABCD15
[7] => https://domain[dot]com/AYuANDEqU40
[8] => Boutique
[9] => Australia
)
[2] => Array
(
[0] => 45672
[1] => Food & Drink
[2] => Free Shipping
[3] => Summer Collection Now. Shop Now!
[4] => 2020-05-17
[5] => 2020-08-01
[6] => N/A
[7] => https://domain[dot]com/AYuANDEqU40761312
[8] => Fancy
[9] => US
)
[3] => Array
(
[0] => 45866
[1] => Food & Drink
[2] => Percentage off
[3] => 20% Off
[4] => 2020-05-17
[5] => 2026-05-15
[6] => OFF20
[7] => https://domain[dot]com/AYuANDEqU40780908
[8] => Biata
[9] => US
)
[4] => Array
(
[0] => 45706
[1] => Electronic Equipment
[2] => Percentage off
[3] => 40% Off-Set Under $200!
[4] => 2020-05-17
[5] => 2026-05-15
[6] => N/A
[7] => https://domain[dot]com/AYuANDEqU40768379
[8] => GVM
[9] => US
)
)
You can easily see that the $newArray
has one different array([4])
from the $oldArray
, and one new array([5])
to the $oldArray
, and one different key=value pair like [1] => Clothing's
from the $oldArray
What i am expecting is to compare the $newArray
with the $oldArray
to get the different array([4])
and the new one array([5])
in the $newArray
only.
I have searched for these links:
https://www.php.net/manual/en/function.array-diff-assoc.php and https://www.w3schools.com/php/func_array_diff_assoc.asp but they do not help.
Then I've searched for the function in this link: Compare two different multidimentional arrays and highlight the changes
The function:
function arrayRecursiveDiff($aArray1, $aArray2) {
$aReturn = array();
foreach ($aArray1 as $mKey => $mValue) {
if (array_key_exists($mKey, $aArray2)) {
if (is_array($mValue)) {
$aRecursiveDiff = arrayRecursiveDiff($mValue, $aArray2[$mKey]);
if (count($aRecursiveDiff)) { $aReturn[$mKey] = $aRecursiveDiff;
}
} else {
if ($mValue != $aArray2[$mKey]) {
$aReturn[$mKey] = $mValue;
}
}
} else {
$aReturn[$mKey] = $mValue;
}
}
return $aReturn;
}
$arr1 = arrayRecursiveDiff($newentry,$oldentry);
It filters and shows me both the COMPLETE new arrays from the $newArray
and the new arrays from the $newArray
with only the key=value pairs that are different from the $oldArray
.
Yes and again, what i am expecting is to compare the $newArray
with the $oldArray
to get:
1/ The COMPLETE different array like array([4])
from the $newArray
. 2/ The new array like array([5])
from the $newArray
. 3/ The arrays from the $newArray
of which one of the key=value pairs is different from that of the $oldArray
, but it must print all 9 key=value pairs of that array, which is the array[0] in this example. I don't want to get the new arrays from $newArray
with the output of ONLY the updated key=value pairs.
Sorry for a long post with wordy explanation, but I have no choice.
Your help is appreciated.
$a1=array(1,2,3);
$a2=array(1,2,4);
$i =0;
$diff_Value = '';
foreach( $a1 as $val )
{
if($a2[$i]!=$val){
$diff_Value.= $val.','.$a2[$i];
}
$i =$i+1;
}
echo $diff_Value;
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.