17

While trying to simulate a bit of PHP behaviour I stumbled across this:

    $a=array(0 => 1, 'test' => 2);
    $b=array('test' => 3, 0 => 1);
    var_dump($a==$b, $a>$b, $b>$a);

According to the output from var_dump $b is bigger than $a. In the PHP manual there is a Transcription of standard array comparison which states that the values of the arrays are compared one by one and if a key from the first array is missing in the second array, the arrays are uncomparable. So far so good. But if I try this (change in the first element of $a only):

    $a=array(0 => 2, 'test' => 2);
    $b=array('test' => 3, 0 => 1);
    var_dump($a==$b, $a>$b, $b>$a);

All three comparison results are false. This looks like "uncomparable" to me (because the > result is the same as the < result, while the arrays are not ==either, which makes no sense) but this does not fit the transcription from the PHP manual. Both keys are present in both arrays and I would expect $a to be bigger this time because the content of key 0 is bigger in $a (2 vs. 1).

I've tried to dig into the PHP source code and found zend_hash_compare() in zend_hash.c, but the code there seems to work as the manual describes.

What's going on here?

5
  • 1
    Intriguingly, if you reverse the order in which the elements are declared, the problem disappears...
    – DaveRandom
    Jan 8, 2012 at 22:21
  • well firstly your trying to make php turn a normal integer array into a mapped array. you should prob. stick with one or the other. I have never had to mix a mapped array with a regular one.. try changing your values when you define the array to tell php that both are to be mapped first. Jan 8, 2012 at 22:22
  • I would guess that there is bug in zend_hash_compare but I can not find it. If you test your arrays with example compare function from manual then you get expected result: $a=array(0 => 2, 'test' => 2); $b=array('test' => 3, 0 => 1); $r = standard_array_compare($a, $b); // c/p from manual var_dump( $r ); result: int(1) This should be reported on bugs.php.net
    – DamirR
    Jan 8, 2012 at 22:28
  • I thought about that, but usually when I suspect a bug in PHP, it's in my head instead ;-) If this turns up nothing, I will try that. Jan 8, 2012 at 22:32
  • I'm not trying to use this in production code, or to solve some problem, I'm just curious as to why this is happening. It's valid syntax and documented behaviour, but just does not add up. Jan 8, 2012 at 22:32

3 Answers 3

6

EDIT: As Joachim has shown, it deals with the order called. To steal his words: "$a>$b loops over b and finds 'test' first. 'test' is greater in $b so $b is greater and it returns false. $b>$a loops over a and finds '0' first. '0' is greater in $a so $a is greater and it returns false."

-- Original Post --

I'm not 100% sure I'm right on this; I haven't seen this before, and have only briefly looked into it (major kudos, by the way, on an excellent question!). Anyway, it would appear that either PHP documentation is wrong, or this is a bug (in which case you might want to submit it), and here is why:

in zend_hash_compare() in zend_hash.c, it seems as though there is some confusion over what ordered is (I'm looking at line 1514 and 1552-1561, which is my best guess is where the problem is, without doing lots of testing).

Here's what I mean; try this:

$a=array(0 => 2, 'test' => 2);
$b=array(0 => 1, 'test' => 3);
var_dump($a==$b, $a>$b, $b>$a);

Note I merely switched the order of indexes, and $a>$b returns true. Also see this:

$x=array(0 => 2, 'test' => 2);
$y = $x;
$y[0] = 1; $y['test'] = 3;
var_dump($x==$y, $x>$y, $y>$x);

Note here, as well, $x>$y returns true. In other words, PHP is not just matching array keys! It cares about the order of those keys in the arrays! You can prevent this situation by coming up with a "base" array and "copying" it into new variables (in my x/y example) before modifying, or you can create an object, if you so desire.

To say all that differently, and much more briefly, it would appear that PHP is not just looking at key values, but at both key values AND key order.

Again, I emphasize I don't know if this expected behavior (it seems like something they ought to have noted in the PHP manual if it was), or a bug/error/etc (which seems much more likely to me). But either way, I'm finding that it is compared first by number of keys (lines 1496-1501 in zend_hash.c), and then by both key value and key order.

9
  • This is most likely not a bug, just a result of which array is foreach'ed through. See my answer for details. Jan 8, 2012 at 22:50
  • Perhaps, but seeing the documentation, specifically looking at "Example #1", it implies in a logical example that order is not of importance. If this is true - if the intent was to have a result despite key order, and yet key order is a factor - then I would personally classify it as a bug.
    – cegfault
    Jan 8, 2012 at 22:58
  • @cegfault Thank you for your detailed thoughts. I've also been over zend_hash_compare() again and again and can't see it. Maybe Joachim is on to something, as the order seems to have impact. Jan 8, 2012 at 23:01
  • @cegfault I'm not quite sure what you mean, the transcript (example #1) uses foreach which indeed does take $op1's array order into account. Am I looking at the same example? :) Jan 8, 2012 at 23:03
  • 1
    I concur that @JoachimIsaksson may be on to something. I don't have time to see where zend_hash_compare is called from. If PHP is indeed juggling the order in which the arrays are passed into zend_hash_compare, then he is most likely right. I would recommend at least posting a comment at PHP, as this is something that should probably at least be known behavior, if not classified as a bug.
    – cegfault
    Jan 8, 2012 at 23:06
5

It would seem that the comparison loop is in the case of > done over the right hand array and in the case of < done over the left hand array, ie always over the supposedly "lesser" array. The order of the elements is significant as the foreach loop in the transcription code respects array order.

In other words;

$a>$b loops over b and finds 'test' first. 'test' is greater in $b so $b is greater and it returns false.

$b>$a loops over a and finds '0' first. '0' is greater in $a so $a is greater and it returns false.

This would actually make sense, the "greater" array is then allowed to contain elements that the "lesser" array doesn't and still be greater as long as all common elements are greater.

3
  • While trying to verify this I found out that, although there is a is_smaller_function() and a is_smaller_or_equal_function() in zend_operators.c, there is no opposite like is_bigger_function() or is_larger_function(). Maybe PHP is indeed, while testing for the larger value, just flipping the arguments and calling the smaller function instead, but I can't find proof for that either. Jan 8, 2012 at 22:53
  • 2
    Found it. Indeed PHP flips the arguments already at the parser stage. The token T_IS_GREATER_OR_EQUAL is transformed as zend_do_binary_op(ZEND_IS_SMALLER_OR_EQUAL...) with reversed parameters. Thanks! Jan 8, 2012 at 23:27
  • @WolfgangStengel Thanks yourself, good to have it confirmed :) Jan 9, 2012 at 5:54
-1

I think here is comparing one by one so $a[0]>$b[0] but $a['test']<$b['test']. You can not say which array is bigger.

5
  • 1
    Please ensure you at least vaguely know what you are talking about before answering questions. Just following the link posted in the question would help...
    – DaveRandom
    Jan 8, 2012 at 22:44
  • @DaveRandom I have read the link before answering. There is written: Array with fewer members is smaller, if key from operand 1 is not found in operand 2 then arrays are uncomparable, otherwise - compare value by value (see following example) but example compare only one value- I think example is not accurate. Its all.
    – Tomas
    Jan 8, 2012 at 22:47
  • The transcription in the manual also just compares one value, if the first one is already different. Jan 8, 2012 at 23:00
  • @Wolfgang Yes you are right, but maybe I do not understand english, but for me value by value sounds like comparing all values and example compares only one
    – Tomas
    Jan 8, 2012 at 23:06
  • I must admit I was wrong. The algorithm compares arrays like words, so my answer is completely wrong.
    – Tomas
    Jan 8, 2012 at 23:22

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge that you have read and understand our privacy policy and code of conduct.

Not the answer you're looking for? Browse other questions tagged or ask your own question.