0

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.

dealerwrx
  • 3
  • 3
  • What is the output of `$diff`? – AbraCadaver Dec 04 '19 at 23:40
  • switch your arrays inside `array_diff()`, i.e. `$diff = array_diff($arry2, $arry1);` – lovelace Dec 05 '19 at 00:06
  • I switched them and it found the difference. But why does it not find it the way I original did it? – dealerwrx Dec 05 '19 at 00:13
  • $diff= array(); it come back blank. – dealerwrx Dec 05 '19 at 00:13
  • If you read the description of this function in the manual (https://www.php.net/manual/en/function.array-diff.php), then you would know why it works like that! **Compares array1 against one or more other arrays and returns the values in array1 that are not present in any of the other arrays.** The field "lineStatus" doesn't exists in your first array, therefore it is not recognized as a difference to your second array where this field exists cause all fields of your first array exists also in your second array. – CodyKL Dec 05 '19 at 02:13
  • @CodyKL - lineStatus is defined in both ... (last item on array1 ... 3rd from bottom in the second) – dealerwrx Dec 05 '19 at 17:52
  • @CodyKL is a little confused, the reason you're getting an empty array with `$diff = array_diff($arry1, $arry2);` is because all **values** found in `$arry1` also exist in `$arry2`. But not the other way around... thus if you reverse the argument arrays to `$diff = array_diff($arry2, $arry1);`, you will get a results array with differences, i.e. all the elements with **values** that exist in `$arry2` and not in `$arry1`. – lovelace Dec 05 '19 at 18:58

1 Answers1

1

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

lovelace
  • 1,195
  • 1
  • 7
  • 10
  • lineStatus exists in both though. There is a lineCode and lineStatus is both. (last item on array1 ... 3rd from bottom in the second) – dealerwrx Dec 05 '19 at 17:51
  • Yes, but a value `W` only exists in `$arry2`, hence it returns `["lineStatus"]=> string(1) "W"` - again, the keys (or key-value combinations) are inconsequential, the function only cares about the values. – lovelace Dec 05 '19 at 18:48
  • `arry1` has ['lineStatus']=C ... `arry2` has ['lineStatus']=W || I have tested this with other arrays and it works correctly, only in this case is it not seeing the difference. - will post another example – dealerwrx Dec 05 '19 at 21:50
  • Put it differently, 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](https://3v4l.org/qoN2t) – lovelace Dec 05 '19 at 22:45
  • Awesome. array_diff uses array_flip. Thanks. if you want to create this as a standalone answer, I will happy give credit! – dealerwrx Dec 06 '19 at 19:20
  • happy I could help! – lovelace Dec 06 '19 at 19:31