php.net |  support |  documentation |  report a bug |  advanced search |  search howto |  statistics |  random bug |  login
Doc Bug #75027 Document correction
Submitted: 2017-08-03 05:41 UTC Modified: 2017-08-03 06:03 UTC
From: vivek_buzruk at yahoo dot com Assigned:
Status: Not a bug Package: Arrays related
PHP Version: 7.1.7 OS: windows
Private report: No CVE-ID: None
 [2017-08-03 05:41 UTC] vivek_buzruk at yahoo dot com
Description:
------------
---
From manual page: http://www.php.net/language.operators.comparison
---
I feel array comparison "standard_array_compare" mentioned in this page need correction. 

If  "!array_key_exists($key, $op2)" returns "null", then "count($op1) < count($op2)" and "count($op1) > count($op2)" should both return "null". If the length of arrays do not match then either of them contain atleast 1 key different / additional than other. Is that right? 

One more observation and comment => comparison is non-type based for Key values, but larger question is should "null" be returned considering difference Key values? Do we consider that both arrays are of different types when "Key" values do not match?

Anyway, depends on the particular need and interpretation, but we can also say that function returns "null" as 2 arrays are non-comparable when Keys do not match.

So, in my view array comparison function should be =>
==================
<?php
// Arrays are compared like this with standard comparison operators
function standard_array_compare($op1, $op2)
{
    if (count($op1) < count($op2) || count($op1) > count($op2)) {
        return null; // uncomparable
    }
    foreach ($op1 as $key => $val) {
        if (!array_key_exists($key, $op2)) {
            return null; // uncomparable
        } elseif ($val < $op2[$key]) {
            return -1;
        } elseif ($val > $op2[$key]) {
            return 1;
        }
    }
    return 0; // $op1 == $op2
}
?>  

Test script:
---------------
Same as example above


Patches

Add a Patch

Pull Requests

Add a Pull Request

History

AllCommentsChangesGit/SVN commitsRelated reports
 [2017-08-03 06:03 UTC] requinix@php.net
-Status: Open +Status: Not a bug
 [2017-08-03 06:03 UTC] requinix@php.net
> Is that right?
No. Two arrays with different lengths will compare showing the shorter array as being "less than" the other. That is how it works. By definition.

And note that returning null in the example function does not mean comparing two arrays natively "returns" null. That's impossible: a comparison is always true or false. Instead it represents that the comparison is not consistent. Sure, the function could be edited to return -1/+1, but IMO that would just create confusion (eg, why one and not the other?) - using null makes the non-comparable nature more clear, even if it's not as technically accurate.

https://3v4l.org/8tR9M
 [2017-08-03 06:03 UTC] requinix@php.net
-Package: PHP Language Specification +Package: Arrays related
 
PHP Copyright © 2001-2024 The PHP Group
All rights reserved.
Last updated: Tue Apr 23 21:01:31 2024 UTC