程式狂想筆記

一個攻城師奮鬥史

0%

php可以做陣列比較

今天無聊試著用array比較
發現竟然var_dump東西竟然是true

依我印象中Java、JavaScript都不能直接做array比較
但是php竟然可以….
太神奇了

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

array也可以用+法,但是+到對印的value還是以左邊為主(本身)
其實自己測試會比較清楚

The + operator returns the right-hand array appended to the left-hand array; for keys that exist in both arrays, the elements from the left-hand array will be used, and the matching elements from the right-hand array will be ignored.

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17

<?php
$a = array("a" => "apple", "b" => "banana");
$b = array("a" => "pear", "b" => "strawberry", "c" => "cherry");

$c = $a + $b; // Union of $a and $b
echo "Union of \$a and \$b: \n";
var_dump($c);

$c = $b + $a; // Union of $b and $a
echo "Union of \$b and \$a: \n";
var_dump($c);

$a += $b; // Union of $a += $b is $a and $b
echo "Union of \$a += \$b: \n";
var_dump($a);
?>

Union of $a and $b:
array(3) {
[“a”]=>
string(5) “apple”
[“b”]=>
string(6) “banana”
[“c”]=>
string(6) “cherry”
}
Union of $b and $a:
array(3) {
[“a”]=>
string(4) “pear”
[“b”]=>
string(10) “strawberry”
[“c”]=>
string(6) “cherry”
}
Union of $a += $b:
array(3) {
‘a’ =>
string(5) “apple”
‘b’ =>
string(6) “banana”
‘c’ =>
string(6) “cherry”
}

array比較 === 跟 ==差別
在於===會檢查key順序

1
2
3
4
5
6
7
8
9
10
11
12
$a = array (0 => "apple", 1 => "banana");
$b = array (1 => "banana", 0 => "apple");

var_dump($a === $b); // prints bool(false) as well

$b = array ("0" => "apple", "1" => "banana");

var_dump($a === $b); // prints bool(true)

$b = array ("0" => "apple-1", "1" => "banana-1");

var_dump($a === $b); // prints bool(false)

PHP: Array Operators - Manual
PHP - Check if two arrays are equal - Stack Overflow
PHP 三十天就上手-Day -17 Array Operators - iT 邦幫忙::一起幫忙解決難題,拯救 IT 人的一天