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)
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
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);
It's to be expected, that
array(10,20,30) != array(10,30,20)
and
array(1,2,3) == array(1,2,3)
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.
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.
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.
Related
I saw a small php quiz online that contained the following code:
$somevalue[[ 2 <=['-']=> 2][1]] = $somestring;
My question is, what does the part before the assignment do?
$somevalue[[ 2 <=['-']=> 2][1]]
<= looks like the comparison operator but in that case it is comparing 2 to '-'?
PHP's array initialization syntax looks like this:
$arr = [ key => value ];
So in this part:
2 <=['-']=> 2
The 'key' is the result of the expression 2 <= ['-'], which according to this page evaluates to true (an array is always greater than what you are comparing it to, unless it's another array). Because PHP arrays keys are either integers or strings, the boolean result is implicitly cast to the integer 1, so you end up with:
1 => 2
So the simplified expression:
[ 1 => 2 ][1]
Will evaluate to the second element of the array we've just created (PHP array's are 0-based), so this will simplify to:
2
So at the end we end up with:
$somevalue[2] = $somestring;
To understand this you need to break the statement in parts,
echo 2 <=['-'];//return true
PHP Comparison operator
After this the statement will be
$somevalue[[1 => 2][1]] = $somestring;
Here you see the array index 1 has values 2. After this the last index which is 1, from the array [1 => 2] it will return 2, so finally you will have
$somevalue[2] = $somestring;
Why does the array_reduce() method work differently when adding and multiplying? When I add the array values below, the code produces the expected result: 15. But when I multiply, it returns: 0. Same code... The only difference is that the + sign is switched for the * sign.
function sum($arr){
print_r(array_reduce($arr, function($a, $b){return $a + $b;}));
}
function multiply($arr){
print_r(array_reduce($arr, function($a, $b){return $a * $b;}));
}
sum(array(1, 2, 3, 4, 5)); // 15
multiply(array(1, 2, 3, 4, 5)); // 0
According to documentation, you might wanna try
function multiply($arr){
print_r(array_reduce($arr, function($a, $b){return $a * $b;},1));
}
Here is a quote from this discussion:
The first parameter to the callback is an accumulator where the result-in-progress is effectively assembled. If you supply an $initial value the accumulator starts out with that value, otherwise it starts out null.
According to http://php.net/manual/en/language.operators.array.php:
$a == $b Equality TRUE if $a and $b have the same key/value pairs.
$a === $b Identity TRUE if $a and $b have the same key/value pairs in the same order and of the same types.
As such, I would have expected these two arrays to have equality, but they do not.
$a1=array('a','b');
$a2=array('b','a');
echo(($a1==$a2)?'equal':'not equal');
I could sort the arrays first and they have both equality and identity.
Am I misinterpreting the PHP manual? Does PHP's array equality require elements in the same order?
EDIT. The posted answers perfectly answered the question. Possible workarounds include the following. While not part of my original question, any recommendations on the best way to implement would be appreciated.
<?php
$a1=array('a','b');
$a2=array('b','a');
echo((($a1==$a2)?'equal':'not equal').'<br>');
echo(((array_diff($a1, $a2) === array_diff($a2, $a1))?'equal':'not equal').'<br>');
sort($a1);
sort($a2);
echo((($a1==$a2)?'equal':'not equal').'<br>');
?>
OUTPUT:
not equal
equal
equal
That's Because:
In the first array it's:
0 => a
1 => b
And in the second array it's:
0 => b
1 => a
So the values and the keys are the same, but not as pair!
So if you change the keys in the second array it's TRUE because the value and the key's are the same as pair:
$a1 = array('a','b');
$a2 = array( 1 =>'b', 0 =>'a');
echo(($a1==$a2)?'equal':'not equal');
Output:
equal
Its a good question but as the docs mention. It needs the same key value pairs. Your first array is 1 => a and your second is 2=>a
Same for b. So therefore not equal.
An example would be
$a=array('a'=>'a','b'=>'b');
$a=array('b=>'b','a'=>'a');
I'm reading about php and it says,
== is Equality such that $a == $b is true if $a and $b have the same elements.
=== is Identity such that $a === $b is true if $a and $b have the same elements, with the same types, in the same order.
So, I thought I'd try and see the difference myself and wrote with this little script:
$a = array(1, 2, 3);
$b = array(2, 3, 1);
if ($a==$b) {echo "yeehaw!";} else {echo "nope";}
if ($a===$b) {echo "yup";} else {echo "nope";}
My thought was that the same order wasn't required for two arrays to be equal. However, when I ran this, I got "nope" and "nope".
What is the difference?
The arrays you've provided have the same set of values, but different key-value-pairs.
Try the following use case instead (same key-value-pairs in different order):
$a = array(0=>1, 1=>2, 2=>3);
$b = array(1=>2, 2=>3, 0=>1);
... and the following use case (different data types):
$a = array(1, 2, 3);
$b = array('1', '2', '3');
The documentation[PHP.net] says:
== TRUE if $a and $b have the same key/value pairs.
=== TRUE if $a and $b have the same key/value pairs in the same order and of the same types.
Since your two arrays are not in the same order1, they don't have the same key-value pairs.
var_dump($a);
array(3) {
[0]=> int(1)
[1]=> int(2)
[2]=> int(3)
}
var_dump($b);
array(3) {
[0]=> int(2)
[1]=> int(3)
[2]=> int(1)
}
1 With regards to their construction via array(), which will index the arguments starting with 0.
My thought was that the same order wasn't required for two arrays to be equal.
To make it clear what the documentation meant by same key/value pairs, let's take a look at the actual array contents:
$a = array(
0 => 1,
1 => 2,
2 => 3,
);
$b = array(
0 => 2,
1 => 3,
2 => 1,
);
Clearly, the pairs are different.
So what about that "same order"?
To illustrate that, let's create $b a little different:
$b => array(
2 => 3,
1 => 2,
0 => 1,
);
The == equality will be satisfied because the pairs are now the same. However, because arrays in PHP are ordered maps, a difference in pair order causes the === equality to fail.
Two arrays are considered identical === if:
number of elements is the same
all data types are the same
all elements are in the same order
each array has the same key-value pairs
What is the difference?
The difference between two arrays can mean different things, so this question is normally best answered by using the kind of difference function for arrays that match your expectations.
In your case, equality is (probably) satisfied by the array_diff() function:
!array_diff($a, $b) && !array_diff($b, $a);
If you say no, that's not what I'm looking for, please see the duplicate question "PHP - Check if two arrays are equal" I also left an extended answer there that shows the other possible differences and how to test for those as you're concerned about comparing values and not element (which are key/value pairs).
1st one fails because the elements are different. 2nd one fails because elements are different although type is same. (both should same)
Is there a way to only keep the first N (for example 10) elements of an array? I know there is array_pop, but is there a better, more elegant way?
You can use array_slice or array_splice:
$b = array_slice($a, 0, 10);
$c = array_splice($a, 0, 10);
Note that array_slice copies the items of $a and returns them while array_splice does modify $a itself and only returns the items that have been removed from $a.