php.net |  support |  documentation |  report a bug |  advanced search |  search howto |  statistics |  random bug |  login
Doc Bug #77487 array_diff_assoc
Submitted: 2019-01-18 21:05 UTC Modified: 2019-01-18 21:39 UTC
From: felipegryllo at gmail dot com Assigned:
Status: Not a bug Package: *General Issues
PHP Version: 5.6.40 OS:
Private report: No CVE-ID: None
View Add Comment Developer Edit
Welcome! If you don't have a Git account, you can't do anything here.
You can add a comment by following this link or if you reported this bug, you can edit this bug over here.
(description)
Block user comment
Status: Assign to:
Package:
Bug Type:
Summary:
From: felipegryllo at gmail dot com
New email:
PHP Version: OS:

 

 [2019-01-18 21:05 UTC] felipegryllo at gmail dot com
Description:
------------
Hello, everyone!

Please, check this description:

"Two values from key => value pairs are considered equal only if (string) $elem1 === (string) $elem2 . In other words a strict check takes place so the string representations must be the same."

But this example say:

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

Returns:

Array
(
    [0] => 0
    [1] => 1
    )

See it: "are considered equal only if (string) $elem1 === (string) $elem2"

if (2 === "2")

It's false! But this function return like equals...

Thanks, so mutch!


Patches

Add a Patch

Pull Requests

Add a Pull Request

History

AllCommentsChangesGit/SVN commitsRelated reports
 [2019-01-18 21:07 UTC] requinix@php.net
-Status: Open +Status: Not a bug
 [2019-01-18 21:07 UTC] requinix@php.net
Thank you for taking the time to write to us, but this is not
a bug. Please double-check the documentation available at
http://www.php.net/manual/ and the instructions on how to report
a bug at http://bugs.php.net/how-to-report.php

Now that you've read the description, read the return values section.
> Returns an array containing all the values from array1 that are not present in any of the other arrays.
 [2019-01-18 21:09 UTC] requinix@php.net
...or are you saying it should return the 2?

(string)2 is "2"
(string)"2" is "2"

"2" === "2" is true
 [2019-01-18 21:18 UTC] 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:20 UTC] requinix@php.net
"are considered equal only if (string) $elem1 === (string) $elem2"
 [2019-01-18 21:27 UTC] 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:39 UTC] 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:47 UTC] felipegryllo at gmail dot com
I'm sorry for that!

I had not understood that "everything" would be converted to string before the comparison!

Thanks for listening!
 
PHP Copyright © 2001-2024 The PHP Group
All rights reserved.
Last updated: Sat Jun 01 18:01:28 2024 UTC