Doc #77487 [Com]: array_diff_assoc

From: Date: Fri, 18 Jan 2019 21:47:58 +0000
Subject: Doc #77487 [Com]: array_diff_assoc
References: 1  Groups: php.doc.bugs 
Request: Send a blank email to doc-bugs+get-16348@lists.php.net to get a copy of this message
Edit report at https://bugs.php.net/bug.php?id=77487&edit=1 ID: 77487 Comment by: felipegryllo at gmail dot com Reported by: felipegryllo at gmail dot com Summary: array_diff_assoc Status: Not a bug Type: Documentation Problem Package: *General Issues PHP Version: 5.6.40 Block user comment: N Private report: N New Comment: I'm sorry for that! I had not understood that "everything" would be converted to string before the comparison! Thanks for listening! Previous Comments: ------------------------------------------------------------------------ [2019-01-18 21:39:11] requinix@php.net Why are you ignoring the "(string)" in the description? (string)$elem1 means to cast the value of $elem1 to a string. (string)$elem2 means to cast the value of $elem2 to a string. === means to perform a strict comparison between the left and right values. So (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. I don't know if I can explain this any better. ------------------------------------------------------------------------ [2019-01-18 21:27:52] felipegryllo at gmail dot com So, the array_diff_assoc should return 2 on this case. Please, check this example posted on manual: http://php.net/manual/en/function.array-diff-assoc.php See: <?php $array1 = array(0, 1, 2); $array2 = array("00", "01", "2"); $result = array_diff_assoc($array1, $array2); print_r($result); ?> Return: Array ( [0] => 0 // (int) 0 isn't equal to (string) "00" or (string) "0" [1] => 1 // (int) 1 isn't equal to (string) "01" or (string) "1" ) Like (int) 2 isn't equal to (string) "2". But it isn't returned. ------------------------------------------------------------------------ [2019-01-18 21:20:01] requinix@php.net "are considered equal only if (string) $elem1 === (string) $elem2" ------------------------------------------------------------------------ [2019-01-18 21:18:51] felipegryllo at gmail dot com Thanks for your faster anwer. But (int) 2 is not identical to (string) 2, like description says... <?php $array1 = array(0, 1, 2); // Here, 2 is integer $array2 = array("00", "01", "2"); // Here, 2 is string $result = array_diff_assoc($array1, $array2); // So, this function should return 2 print_r($result); ?> Like ((int) 2 === (string) 2) returned false... See this var_dump: var_dump($array1); /* * Return: * array(3) { * [0]=> * int(0) * [1]=> * int(1) * [2]=> * int(2) * } *************************/ var_dump($array2); /* * Return: * array(3) { * [0]=> * string(2) "00" * [1]=> * string(2) "01" * [2]=> * string(1) "2" * } *************************/ Thanks! ;) ------------------------------------------------------------------------ [2019-01-18 21:09:07] requinix@php.net ...or are you saying it should return the 2? (string)2 is "2" (string)"2" is "2" "2" === "2" is true ------------------------------------------------------------------------ The remainder of the comments for this report are too long. To view the rest of the comments, please view the bug report online at https://bugs.php.net/bug.php?id=77487 -- Edit this bug report at https://bugs.php.net/bug.php?id=77487&edit=1

« previous php.doc.bugs (#16348) next »