2

Code:

$arr = array( 10, 20, 30 );
$arr1 = array(
       1=>30,
       2=>20,
       0=>10
);
var_dump( $arr == $arr1 );

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

This outputs:

bool(false)
bool(true)
Kent Fredric
  • 56,416
  • 14
  • 107
  • 150

6 Answers6

6

Two arrays will be considered equal if their corresponding values are the same.

In your first example you are comparing two arrays:

[10, 20, 30]
[10, 30, 20]

Obviously these are not the same, so it returns false. The second example though:

[1, 2, 3]
[1, 2, 3]

...are the same. Am I missing something here?


If you want to test if two arrays have the same members, see this question: Algorithm to tell if two arrays have identical members

If you just want to see they have the same totals, you can use array_sum

Community
  • 1
  • 1
nickf
  • 537,072
  • 198
  • 649
  • 721
  • this is a equivalence operator it should consider only values and not the index , if it is an identity operator(===) then it should consider order as well –  Mar 24 '09 at 12:45
  • @jackie: in array values do not exist w/o index. – SilentGhost Mar 24 '09 at 12:48
2

If you do not specify keys for array, php automatically selects numbers, starting with 0. Therefore, the following pairs of lines mean the same:

$arr = array(10,20,30);
$arr = array(0=>10,1=>20,2=>30);

$a = array(1,2,3);
$a = array(0=>1,1=>2,2=>3);
phihag
  • 278,196
  • 72
  • 453
  • 469
  • this is a equivalence operator it should consider only values and not the index , if it is an identity operator(===) then it should consider order as well –  Mar 24 '09 at 12:48
0

It's to be expected, that

array(10,20,30) != array(10,30,20)

and

array(1,2,3) == array(1,2,3)
vartec
  • 131,205
  • 36
  • 218
  • 244
0

Thats a very broad question, but in the case of an array, it compares on an index-by-index basis.

In your first block of code, $arr is not equal to $arr1 because 30 != 10 and 10!=30.

In the second block of code, you are specifying that at index 0, the value is 1. At index 1, the value is 2, and at index 2, the value is 3. Thus, you have the same array.

JoshJordan
  • 12,676
  • 10
  • 53
  • 63
  • this is a equivalence operator it should consider only values and not the index , if it is an identity operator(===) then it should consider order as well –  Mar 24 '09 at 12:44
  • Well, in PHP you cannot have an array value without an index. Are you saying that (1,2,3) and (3,2,1) are equivalent? – JoshJordan Mar 24 '09 at 13:53
0

Given some of your comments, I think it would be useful if you read up on array type in php. Mainly the fact that there is no key-less arrays.
And don't forget comparison operators as well.

SilentGhost
  • 307,395
  • 66
  • 306
  • 293
  • hey thanks it is very useful as got the difference between order and keys of an array –  Mar 24 '09 at 13:15
0

th equality operator is === in php and within an array => is correct. also $arr !=$arr1 coz 20 !=30, 30!=20 as per you have assigned.

terrific
  • 1,637
  • 3
  • 15
  • 14