Would anyone know the reasons why number two doesn’t appear on
Array printing, if types are not the same?
The number 2 does not appear in your print_r
precisely because of the cast (String) that the method performs before making the comparison:
Two key pair values => value are considered equal only if (String) $elem1 === (String) $elem2 . In other words, a strict String check is done so that String representations are the same.
Simplifying for you, this method makes the comparison of two Arrays
getting its values by index of Array
and transforming them into String
before making the comparison, ie to improve their understanding by calling array_diff_assoc($array1, $array2);
it makes the 3 following steps:
$array1[0] em seu cast para String é igual(em tipo e valor ===) à $array2[0] ? Se não, vamos exibí-lo,
$array1[1] em seu cast para String é igual(em tipo e valor ===) à $array2[1] ? Se não, vamos exibí-lo,
$array1[2] em seu cast para String é igual(em tipo e valor ===) à $array2[2] ? Se não, vamos exibí-lo.
In the latter case, the conversion value 2 (from $array1) to String and the value 2 (from $array2) are equal in value and typing, so it is not displayed in your print_r
.
It is worth remembering that the use of array_diff_assoc()
also checks the index equality of its Array
.
@Edit:
In contact with the PHP team, we got the following reply:
You’re ignoring the "(string)
" in the Description...
(string) $elem1
Means to cast the value of $elem1
to string
.
(string) $elem2
Means to cast the value of $elem2
to string
.
===
Means to perform a Strict comparison between the left and right values.
Only (string) $elem1 === (string) $elem2
Means to perform a Strict comparison between the left, which is the value of $elem1
cast to a string
, and the right, which is the value of $elem2
cast to a string
.
If $elem1
is 2
and $elem2
is "2"
then (string) $elem1 === (string) $elem2
is the same as (string) 2 === (string) "2"
is the same as "2" === "2"
which is true.
array_diff_assoc
will compare the values in Multiple arrays According to their Keys, and Return the ones in the first array that are not present in the other arrays. Since the comparison was true for $array1
's 2=>2
entry, array_diff_assoc
considers that the value 2
was present in the Second array and so it will not include it in the Return value.
So no, it should not Return 2
.
In free translation:
Are you ignoring the "(string)
" in the description ...
(string) $elem1
means to convert the value of $elem1
for a string
.
(string) $elem2
means to convert the value of $elem2
for a string
.
===
means making a strict comparison between left and right values.
So, (string) $elem1 === (string) $elem2
means to perform a strict comparison between the left, which is the value of $elem1
converted into a string
, and the right, which is the value of $elem2
converted into a string
.
If $elem1
for 2
and $elem2
for "2"
then (string) $elem1 === (string) $elem2
is the same as (string) 2 === (string) "2"
is the same as "2" === "2"
that is true.
array_diff_assoc
will compare the values in multiple arrays according to your keys, and will return those on the first array who are not present in others arrays. As the comparison was true for the entrance 2 => 2
of $array1
, the array_diff_assoc
considers that the value 2
was present in the second array and therefore will not include it in the return value.
Then no, you must not return 2
.
For [email protected] in response to @Lipespry.
I just got a response from the PHP developers that explains exactly that. If you find it interesting, I can reinforce your response by adding such an explanation...
– LipESprY
Of course Lip, the more explanatory, the better for everyone!
– RXSD
Thank you @Andréfilipe helped me a lot!!!!
– Luiz Augusto
Done! @Luizaugusto see edited section.
– LipESprY
Personal ball show!
– RXSD