PHP太空飞船操作员如何精确比较字符串,数组和对象 [英] How exactly php spaceship operator compare strings, arrays and objects

查看:107
本文介绍了PHP太空飞船操作员如何精确比较字符串,数组和对象的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想知道php宇宙飞船操作员如何比较字符串,对象和数组.例如,下面的代码.

I am wondering how the php spaceship operator compares strings, objects and arrays. For example, the below code.

echo "Its Me at SO" <=> "Its Me at SO"; 

将返回0,因为我知道所有字符都相同,计数相同.但是,如果我有如下代码:

will return 0, as i know all characters are same, count is same. But if i have a code like below:

echo "Its me at SO" <=> "its Me at so"; 

它将返回1,表示左侧大于右侧,但是如何?它在比较ASCII值吗?

It will return 1, means that left side is greater than right side, but how? Is it comparing the ASCII values?

现在让我们进入数组.下面的代码将返回0,因为两个数组的计数,值和每个索引的值均相等.

Now lets come to arrays. The below code will return 0, as both arrays are equal by count, values and values at each index.

echo [1,2,3] <=> [1,2,3];

但是下面的代码返回-1

But the below code returns -1

echo [1,2,3] <=> [3,2,1]; 

我不明白为什么?此运算符如何比较数组,以及如何计算左侧的数组小于右侧的数组? 对于对象也是如此.

And i dont understand why? How this operator compares the arrays and how it calculates that the array on left is smaller than the array on right? And the same goes for the objects.

任何人都可以给出详细的答案来说明它如何与字符串,数组和对象一起工作吗?

Can anybody give a detailed answer that how it works with strings, arrays and objects?

谢谢

推荐答案

比较是根据PHP常用的类型比较规则(

"Comparisons are performed according to PHP's usual type comparison rules (http://php.net/manual/en/types.comparisons.php)".

1)是,它使用ASCII值

1) Yes, it uses the ASCII values

2)如果数组的长度不同,则值较小的数组会更小.

2) If the arrays are different lengths, the Array with fewer values is smaller.

否则,它会逐个键地比较数组,并赋予较早的"值优先.例如,首先比较$arr1[0]$arr2[0].如果$arr1具有$arr2中不存在的键,则这些数组是不可比较的(例如,如果我们使用的是非数字数组).

Otherwise it compares the arrays key by key, giving "earlier" values priority. For example comparing $arr1[0] to $arr2[0] first. If $arr1 has a key that doesn't exist in $arr2, the arrays aren't comparable (eg if we're using non-numeric arrays).

// 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的实际代码,只是所用逻辑的近似表示.

Note, the above is not PHP's actual code, just an approximate representation of the logic used.

然后,从本质上讲,它以类似于比较大端数字的方式对待数组.它将$arr1[0]$arr2[0]进行比较.如果它们不同,则根据更大的值返回-1或1.如果它们相同,则移至$arr1[1]$arr2[1].如果所有值都相同,则返回0(数组相等)

Essentially, then, it treats an array in a similar way to comparing a big-endian number. It compares $arr1[0] to $arr2[0]. If they are the different it returns -1 or 1 depending which is larger. If they are the same it moves on to $arr1[1] and $arr2[1]. If all values are the same it returns 0 (arrays are equal)

虽然不完全相同,但将[1,2,3] <=> [3,2,1]基本上等同于123 <=> 321...

While not exactly the same, it might be simpler to consider [1,2,3] <=> [3,2,1] as basically equivalent to 123 <=> 321...

这篇关于PHP太空飞船操作员如何精确比较字符串,数组和对象的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆