dealerwrx
dealerwrx

Reputation: 3

array_diff() doesn't respect keys when comparing values between two flat associative arrays

I have two arrays:

(
    [lineCode] => C
    [serviceRequest] => Ext, Warr auth.#19091100102 for $650.64 to be paid by c/card(fax#817-785-6700). Cust owes balance
    [skillCode] => 90SB
    [opCode] => 90SB
    [jobType] => CUSTOMER
    [techNo] => 
    [lineStatus] => C
)
(
    [id] => 755350
    [rid] => 252178
    [lineCode] => C
    [serviceRequest] => Ext, Warr auth.#19091100102 for $650.64 to be paid by c/card(fax#817-785-6700). Cust owes balance
    [skillCode] => 90SB
    [opCode] => 90SB
    [jobType] => CUSTOMER
    [techNo] => 
    [lineStatus] => W
    [timeA] => 1575497139
    [timeC] => 0
)

When i perform $diff = array_diff($arry1, $arry2);, it does not find the lineStatus to be different. Could it be because of service Request line with special characters? Although as a test, I set both lines to blanks, and it still did not see the difference.

Any help would be great. Stumped for the day.

UPDATE w/ MORE INFO As this is part of a larger loop through mulitple arrays, the diff check before this one above is below:

$arry1= Array
(
    [lineCode] => B
    [serviceRequest] => 
    [skillCode] => 15
    [opCode] => 15
    [jobType] => CUSTOMER
    [techNo] => A05
    [lineStatus] => C
)
$arry2= Array
(
    [id] => 755362
    [rid] => 252184
    [lineCode] => B
    [serviceRequest] => 
    [skillCode] => 15
    [opCode] => 15
    [jobType] => CUSTOMER
    [techNo] => A05
    [lineStatus] => W
    [timeA] => 1575504138
    [timeC] => 0
)
$diff= Array
(
    [lineStatus] => C
)

the code for the diff is $diff=array_diff($arry1,$arry2);

If it works correctly for this one, why would it not for the next.

Upvotes: 0

Views: 1389

Answers (1)

lovelace
lovelace

Reputation: 1205

Why does it not find it the way you originally did it...?

array_diff() does not care about key-value combinations, it only compares the values in the different arrays that are compared.

To better understand what's happening: if in your updated data you introduce an array element [dummy] => "C" into $arry2 you will no longer get [lineStatus] => "C" returned into $diff. As the value C is now found in both $arry1 and $arry2.

Have a look at this demo

Upvotes: 1

Related Questions