Doc #75027 [Opn->Nab]: Document correction
| From: | requinix@php.net | Date: | Thu, 03 Aug 2017 06:03:38 +0000 |
| Subject: | Doc #75027 [Opn->Nab]: Document correction | ||
| References: | 1 | Groups: | php.doc.bugs |
| Request: | Send a blank email to doc-bugs+get-14879@lists.php.net to get a copy of this message | ||
Edit report at https://bugs.php.net/bug.php?id=75027&edit=1
ID: 75027
Updated by: requinix@php.net
Reported by: vivek_buzruk at yahoo dot com
Summary: Document correction
-Status: Open
+Status: Not a bug
Type: Documentation Problem
Package: PHP Language Specification
Operating System: windows
PHP Version: 7.1.7
Block user comment: N
Private report: N
New Comment:
> 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
Previous Comments:
------------------------------------------------------------------------
[2017-08-03 05:41:10] 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
------------------------------------------------------------------------
--
Edit this bug report at https://bugs.php.net/bug.php?id=75027&edit=1