Basic doubt about function diff Assoc - PHP

Asked

Viewed 61 times

1

Practicing and remembering the functions in PHP, found a little doubt in the function array_diff_assoc.

Example of the site:

Example #2 Example of the function array_diff_assoc()

Two key pair values => value are considered equal only if (String) $elem1 === (String) $elem2 . In other words, a strict checking of string is done so that representations of String be the same.

Could someone tell me why number two doesn’t appear on the Array, if the guys aren’t the same?

<?php
$array1 = array(0, 1, 2);
$array2 = array("00", "01", "2");
$result = array_diff_assoc($array1, $array2);
print_r($result);
?>

2 answers

5

Because PHP is a language of weak typing and tries to interpret everything that gives as the same type, so 2 is the same as "2". If this bothers you look for a strong typing language. PHP rules are weird and when you have one 0 on the front he interprets different. So in this language can never trust the given.

More can be seen in Why in PHP the expression "2 + '6 apples'" is 8?.

3


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.

  • 2

    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...

  • 2

    Of course Lip, the more explanatory, the better for everyone!

  • 1

    Thank you @Andréfilipe helped me a lot!!!!

  • 2

    Done! @Luizaugusto see edited section.

  • 1

    Personal ball show!

Browser other questions tagged

You are not signed in. Login or sign up in order to post.