域运算符::与成员运算符_PHP中的数组运算符:有趣但口语少

域运算符::与成员运算符

Operators in PHP can be organized into seven different categories: arithmetic, assignment, bitwise, comparison, error control, execution, incrementing/decrementing, logical, string, array, and type operators. This article details working with array operators, but also covers how some of the other operators work when used with arrays.

PHP中的运算符可以分为七类:算术运算,赋值运算,按位运算,比较运算,错误控制,执行,递增/递减运算,逻辑运算符,字符串运算符,数组运算符和类型运算符。 本文详细介绍了与数组运算符一起使用的方法,还介绍了与数组一起使用时其他一些运算符的工作方式。

数组运算符 (Array Operators)

The official documentation provides just a quick description of each of the array operators, which sometimes can make it ambiguous as to what to expect from which each operator. Let’s take a look at each of the array operators for a much clearer understanding of what they do. All these operators are binary, that is each operates on exactly two arrays.

官方文档仅提供了每个数组运算符的快速描述,有时可能会使人对每个数组运算符的期望含糊不清。 让我们看一下每个数组运算符,以更清楚地了解它们的作用。 所有这些运算符都是二进制的,即每个运算符都对两个数组进行运算。

阵列联合 (Array Union)

First is the union operator (+) which gives the union of two arrays based on the arrays’ keys. It does loose key matching and all keys of the second array are ignored if their equivalent key already exists in the first array. The rest of the keys (and the corresponding values) of the second array are appended to the first.

首先是并集运算符( + ),它根据数组的键给出两个数组的并集。 它会进行松散键匹配,并且如果第一个数组中已经存在等效键,则会忽略第二个数组的所有键。 第二个数组的其余键(以及相应的值)将附加到第一个数组。

<?php
$array1 = array('a', 'b', 'c');
$array2 = array('d', 'e', 'f', 'g', 'h', 'i');
print_r($array1 + $array2);
print_r($array2 + $array1);
Array
(
    [0] => a
    [1] => b
    [2] => c
    [3] => g
    [4] => h
    [5] => i
)
Array
(
    [0] => d
    [1] => e
    [2] => f
    [3] => g
    [4] => h
    [5] => i
)

With the first print_r(), the first three elements in $array2 have keys that already exist in $array1, so ‘d’, ‘e’, and ‘f’ are ignored in the resulting array. With the second print_r(), all of the keys of $array1 already exist in array $array2 so all of its elements are ignored.

对于第一个print_r()$array2的前三个元素具有$array1中已经存在的键,因此在结果数组中将忽略'd','e'和'f'。 使用第二个print_r() ,$ array1的所有键已经存在于数组$array2因此其所有元素都将被忽略。

The loose matching behavior may give you totally unexpected results, but also opens up exciting opportunities for optimization and loose coding.

松散的匹配行为可能会给您完全出乎意料的结果,但同时也为优化和松散的编码带来了令人兴奋的机会。

<?php
$array1 = array('0' => 'a', '1' => 'b', '2' => 'c', '3' => 'd');
$array2 = array(false => 'e', 1 => 'f', 2 => 'g', 3 => 'h', 4 => 'i');
print_r($array1 + $array2);
Array
(
    [0] => a
    [1] => b
    [2] => c
    [3] => d
    [4] => i
)

It is often misunderstood that the union is based on the array values, while in reality this operator implements the union of array keys. For value-based union, you can use a combination of array_merge() and array_unique():

人们常常误解了联合是基于数组值的,而实际上,该运算符实现了数组键的联合。 对于基于值的联合,可以结合使用array_merge()array_unique()

<?php
$union = array_unique(array_merge($array1, $array2));
print_r($union);
Array
(
    [0] => a
    [1] => b
    [2] => c
    [3] => d
    [4] => e
    [5] => f
    [6] => g
    [7] => h
    [8] => i
)

数组相等 (Array Equality)

The equality operator (==) checks whether two arrays are similar. The operator returns true if all key-value pairs in first array have equivalent key-value pairs in the second array. It matches values and keys loosely and the sequence of elements is ignored.

等于运算符( == )检查两个数组是否相似。 如果第一个数组中的所有键值对在第二个数组中具有等效的键值对,则运算符返回true。 它宽松地匹配值和键,并且忽略元素序列。

<?php
$array1 = array('1' => 1, '2' => 2, '3' => 3, '0' => 0);
$array2 = array(false => '0', 1 => '1', 2 => '2', 3 => '3');
var_dump($array1 == $array2);
bool(true)

The sequence of elements in the two arrays is different, but the same values are tied to similar keys in each array. However, the following are not equivalent because both arrays have different key value pairs:

两个数组中元素的顺序不同,但是相同的值绑定到每个数组中的相似键。 但是,以下内容并不等效,因为两个数组都有不同的键值对:

<?php
$array1 = array(1, 2);
$array2 = array(2, 1);
var_dump($array1 == $array2);
bool(false)

The inequality operator (!= or <>) checks whether two arrays are not similar, and is a perfect opposite of the equality operator. Anything that the equality operator returns false for, this operator returns true, and vice versa.

不等式运算符( !=<> )检查两个数组是否不相似,并且与等式运算符完全相反。 等于运算符返回为false的任何内容,此运算符返回true,反之亦然。

<?php
$array1 = array('1' => 1, '2' => 2, '3' => 3, '0' => 0);
$array2 = array(false => '0', 1 => '1', 2 => '2', 3 => '3');
var_dump($array1 != $array2);
bool(false)

阵列识别 (Array Identity)

The identity operator (===) checks if two arrays are identical. The arrays are identical if both arrays:

身份运算符( === )检查两个数组是否相同。 如果两个数组都相同,则它们是相同的:

  • have the same number of elements

    具有相同数量的元素
  • have the same key-value pairs

    具有相同的键值对
  • have the same sequence of elements

    具有相同的元素顺序
  • have the same data type of all corresponding values

    具有所有对应值的相同数据类型

However, for array keys, the identity operator matches loosely if a key is an integer and a similar string notation of the integer exists as a key in the other array. For a float vs. string key match, this operator goes strict. The PHP manual does not clarify this difference.

但是,对于数组键,如果一个键是整数,并且另一个数组中的键存在类似的整数符号,则标识运算符松散地匹配。 对于浮点对字符串键匹配,此运算符严格执行。 PHP手册没有阐明这一区别。

<?php
// arrays are almost identical but keys have different types
$array1 = array('0' => '0', '1' => '1', '2' => '2', '3' => '3');
$array2 = array(0 => '0', 1 => '1', 2 => '2', 3 => '3');
var_dump($array1 === $array2);
bool(true)
<?php
// the sequence of elements in both arrays is not the same
$array1 = array('0' => '0', '1' => '1', '2' => '2', '3' => '3');
$array2 = array(1 => '1', 2 => '2', 3 => '3', 0 => '0');
var_dump($array1 === $array2);
bool(false)
<?php
// the key in the first array is a string and in the second is a float
$array1 = array('0.5' => '0');
$array2 = array(0.5 => '0');
var_dump($array1 === $array2);
bool(false)

The non-identity operator (!==) checks if two arrays are not identical. Again, this operator is the exact opposite of the identity operator, which means this operator returns false for a comparison of arrays if they are identical.

非身份运算符( !== )检查两个数组是否不相同。 同样,此运算符与Identity运算符完全相反,这意味着如果数组相同,则此运算符将返回false以比较数组。

<?php
$array1 = array('0' => '0', '1' => '1', '2' => '2', '3' => '3');
$array2 = array(0 => '0', 1 => '1', 2 => '2', 3 => '3');
var_dump($array1 !== $array2);
bool(false)

与其他运算符一起使用数组 (Using Arrays with Other Operators)

PHP behaves differently when operators other than those mentioned above are applied to arrays. Here is a list of those operators and the behavior when they are applied to arrays.

当将除上述运算符之外的其他运算符应用于数组时,PHP的行为会有所不同。 这是这些运算符以及将其应用于数组时的行为的列表。

致命错误:意外的操作数类型 (Fatal Error: Unexpected Operand Type)

PHP issues a fatal error when the following operators are applied to arrays:

将以下运算符应用于数组时,PHP发出致命错误:

  • bitwise-not operator (~)

    按位非运算符( ~ )

  • arithmetic negation Operator (-)

    算术求反运算符( - )

  • arithmetic subtraction operator (-)

    算术减法运算符( - )

  • arithmetic multiplication operator (*)

    算术乘法运算符( * )

  • arithmetic division operator (/)

    算术除法运算符( / )

视为整数的数组 (Arrays Treated as Integers)

Arrays are treated as integers when used with the following operators. An empty array (with no elements) is considered as int(0) and non-empty arrays are considered int(1).

与以下运算符一起使用时,将数组视为整数。 空数组(无元素)被视为int(0) ,非空数组被视为int(1)

  • Logical-not (!) returns true for an empty array and false when the operand array has one or more elements.

    对于空数组,逻辑非( ! )返回true,而当操作数数组具有一个或多个元素时,则返回false。

  • Bitwise-and (&) returns 1 if both of its operands are non-empty and 0 if one or both of its operands are empty.

    如果两个操作数都不为空,则按位与( & )返回1;如果两个操作数之一或全部为空,则返回0。

  • Bitwise-or (|) returns 0 if both of its operands are empty, otherwise it returns 1.

    如果两个操作数均为空,则按位或( | )返回0,否则返回1。

  • Bitwise-xor (^) returns 0 if both arrays are either empty or filled. If one of the arrays is empty, it returns 1.

    如果两个数组均为空或已填充,则按位异或( ^ )返回0。 如果数组之一为空,则返回1。

  • Shifting an array n-steps to the left with the left-shit operator (<<) returns the integer equivalent of 1 << n if the array is non-empty. Otherwise it returns 0 (the outcome of 0 << n).

    如果数组是非空的,则使用左击运算符( << )将数组向左移动n步将返回等价于1 << n的整数。 否则返回0(结果0 << n )。

  • The right-shift operator (>>) has similar behavior as left-shift except that it shifts rightward.

    右移运算符( >> )与左移具有相似的行为,只不过它向右移动。

  • Modulus (%) returns true if both of the arrays are non-empty. If the second array is empty, it issues a “Division by Zero” error. If the first array is empty, it returns 0 (the result of 0 % 1).

    如果两个数组均为非空,则模量( % )返回true。 如果第二个数组为空,则会发出“被零除”错误。 如果第一个数组为空,则返回0( 0 % 1的结果)。

  • Logical-and (&& and AND) return false if any of the arrays are empty. If both arrays are non empty, it returns true.

    如果任何数组为空,则逻辑-和( &&AND )返回false。 如果两个数组都不为空,则返回true。

  • Logical-or (|| and OR) return true if any of the operand arrays are non-empty. If both arrays are empty, it returns false.

    如果任何操作数数组为非空,则逻辑或( ||OR )返回true。 如果两个数组均为空,则返回false。

  • If both of the arrays are either empty or non empty, logical-xor (XOR) returns false. Otherwise, it returns true if one of the arrays is empty.

    如果两个数组均为空或非空,则逻辑异XOR ( XOR )将返回false。 否则,如果数组之一为空,则返回true。

  • Casting an array to bool gives false if the array is empty, otherwise true.

    如果数组为空,则将数组bool转换为bool将返回false,否则返回true。

视为字符串的数组 (Arrays Treated as Strings)

When concatenating two arrays, the string concatenation operator (.) takes each array as the string “Array” and concatenates these strings.

当连接两个数组时,字符串连接运算符( . )将每个数组作为字符串“ Array”并连接这些字符串。

<?php
$array1 = array(0, 1, 2, 3);
$array1 = array(0, 1, 2, 3);
var_dump($array1 . $array2);
string(10) "ArrayArray"

没有效果 (No Effect)

The incrementing/decrementing category of operators (++ and --) have no impact on arrays.

运算符的增减类别( ++-- )对数组没有影响。

<?php
$array1 = $array2;
var_dump((++$array1) === $array2);
bool(true)

结论 (Conclusion)

The actual documentation on how PHP’s operators behave when used with arrays is sparse, but for more information you can look at the user submitted comments in the array operators page. Your questions and comments are welcome here too, and I shall happily explain things further.

有关与数组一起使用时PHP运算符的行为的实际文档很少,但是有关更多信息,您可以在数组运算符页面中查看用户提交的注释。 您的问题和评论也欢迎在这里,我将很高兴为您进一步解释。

Image via Fotolia

图片来自Fotolia

翻译自: https://www.sitepoint.com/array-operators-in-php-interesting-but-less-spoken/

域运算符::与成员运算符

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值