我想知道 php spaceship 运算符如何比较字符串、对象和数组。例如下面的代码。
echo "Its Me at SO" <=> "Its Me at SO";
将返回 0,因为我知道所有字符都相同,计数也相同。但如果我有如下代码:
echo "Its me at SO" <=> "its Me at so";
它会返回1,表示左侧大于右侧,但是如何呢?是比较 ASCII 值吗?
现在让我们来看看数组。下面的代码将返回 0,因为两个数组的计数、值和每个索引处的值相等。
echo [1,2,3] <=> [1,2,3];
但是下面的代码返回-1
echo [1,2,3] <=> [3,2,1];
我不明白为什么?该运算符如何比较数组以及如何计算左侧数组小于右侧数组? 物体也是如此。
任何人都可以详细回答它如何处理字符串、数组和对象吗?
谢谢你
“根据 PHP 通常的类型比较规则进行比较 (http://php.net/manual/en/types.comparisons.php)”。
1) 是的,它使用 ASCII 值
2) 如果数组长度不同,则值越少的数组越小。
否则,它会逐个键比较数组,并给予“较早”值优先级。例如,首先比较
$arr1[0]
与 $arr2[0]
。如果 $arr1
具有 $arr2
中不存在的键,则数组不具有可比性(例如,如果我们使用非数字数组)。
// Arrays are compared like this with standard comparison operators
// $arr1 and $arr2 are arrays
function standard_array_compare($arr1, $arr2)
{
// If either array has more values, that array is considered "larger"
if (count($arr1) < count($arr2)) {
return -1; // $arr1 < $arr2
} elseif (count($arr1) > count($arr2)) {
return 1; // $arr1 > $arr2
}
//Otherwise compare the array values directly
foreach ($arr1 as $key => $val) {
if (!array_key_exists($key, $arr2)) {
return null; // uncomparable, these arrays do not have the same keys
} elseif ($val < $arr2[$key]) {
return -1; // $arr1 < $arr2
} elseif ($val > $arr2[$key]) {
return 1; // $arr1 > $arr2
}
}
return 0; // $arr1 == $arr2
}
注意,上面不是 PHP 的实际代码,只是所使用逻辑的近似表示。
本质上,它处理数组的方式与比较大端数字类似。它将
$arr1[0]
与 $arr2[0]
进行比较。如果它们不同,则返回 -1 或 1,具体取决于哪个较大。如果它们相同,则转到 $arr1[1]
和 $arr2[1]
。如果所有值都相同,则返回 0(数组相等)
虽然不完全相同,但将
[1,2,3] <=> [3,2,1]
视为基本上等同于 123 <=> 321...
可能会更简单
根据新功能文档
根据 PHP 通常的类型比较规则进行比较。
<?php
// Integers
echo 1 <=> 1; // 0
echo 1 <=> 2; // -1
echo 2 <=> 1; // 1
// Floats
echo 1.5 <=> 1.5; // 0
echo 1.5 <=> 2.5; // -1
echo 2.5 <=> 1.5; // 1
// Strings
echo "a" <=> "a"; // 0
echo "a" <=> "b"; // -1
echo "b" <=> "a"; // 1
宇宙飞船数组 php8
<?php
echo [] <=> []; // result is 0
echo [1, 2, 3] <=> [1, 2, 3]; // result is 0
echo [1, 2, 3] <=> []; // 1
echo [1, 2, 3] <=> [1, 2, 1]; // 1
echo [1, 2, 3] <=> [1, 2, 4]; // -1
echo [1,2,3,1] <=> [1,2,4];//1 //thats mean index first piority,1st array index number getter then 2nd array so its positive answer 1
echo [1,2,4] <=> [1,2,3,1]//-1
使用 PHP spaceship 运算符 (<=>) 比较数组、对象或字符串遵循与 其他 PHP 比较运算符相同的规则:
字符串比较:
echo "a" <=> "a"; // 0
echo "a" <=> "b"; // -1
echo "b" <=> "a"; // 1
echo "a" <=> "aa"; // -1
echo "zz" <=> "aa"; // 1
数组比较
// array with fewer elements is considered smaller
echo [1, 2] <=> [1, 2, 3]; // -1
echo [1, 2, 3] <=> [1, 2]; // 1
// arrays with same length but different keys are incomparable, always returning 1
echo ['a' => 1, 'b' => 2, 'c' => 3] <=> ['a' => 1, 'b' => 2, 'd' => 4]; // 1
echo ['a' => 1, 'b' => 2, 'c' => 3] <=> ['a' => 1, 'b' => 2, 'd' => 4]; // 1
// arrays with same length and keys return comparison result of first non-equal value
echo ['a' => 1, 'b' => 2, 'c' => 4] <=> ['a' => 1, 'b' => 3, 'c' => 2]; // -1
// arrays with same length and key/value pairs are equal, regardless of order
echo ['a' => 1, 'b' => 2, 'c' => 3] <=> ['c' => 3, 'b' => 2, 'a' => 1]; // 0
为那些有兴趣了解更多信息的人写了一篇博客文章
// objects of different classes are incomparable, always returning 1
class Foo {}
class Bar {}
echo new Foo() <=> new Bar(); // 1
echo new Bar() <=> new Foo(); // 1
// objects of same class with different property values return first non-equal result
class Car
{
public function __construct(
private string $name,
) {}
}
$car1 = new Car('Porsche');
$car2 = new Car('Lamborghini');
echo $car1 <=> $car2; // 1
echo $car2 <=> $car1; // -1
// objects of same class with same property values are equal
class Car
{
public function __construct(
private string $name,
) {}
}
$car1 = new Car('Porsche');
$car2 = new Car('Porsche');
echo $car1 <=> $car2; // 0
echo $car2 <=> $car1; // 0
为那些有兴趣了解更多信息的人写了一篇博客文章