• Tutorial List

PHP Array Operators

Last updated May 15, 2020
PHP Array Operators

Table of PHP Array Operators

πŸ“ Except for the union Operator, the PHP array Operators are used to compare arrays.

OperatorNameExampleResult
+Union
$x + $y
Union of $x and $y
==Equality
$x == $y
Returns TRUE if $x and $x have the same key/value pairs (Orders do not matter).
===Identity
$x === $y
Returns TRUE if $x and $y have the same key/value pairs in the same order and of the same types.
!=Inequality
$x != $y
Returns TRUE if $x is not equal to $x.
<>Inequality
$x <> $y
Returns TRUE if $x is not equal to $x.
!==Non-identity
$x !== $y
Returns TRUE if $a is not identical to $b.
PHP Array Operators

Array Operators Example

+ Union Operator Example:

  1. The Union (+) operator returns the right-hand array appended to the left-hand array.
  2. If a key of an element exists in both arrays, then the element from the left-hand array will be used, and the matching element from the right-hand array will be ignored.
<?php
  $left_side = array('name' => 'John Doe', 'age' => 22);
  $right_side = array('name' => 'Barry Allen', 'age' => 28, 'email' => '[email protected]');

  $result = $left_side + $right_side;

  var_dump($result);

  /* RESULT --

  $result = array(
      'name' => 'John Doe',
      'age' => 22,
      'email' => '[email protected]'
  );

  */
?>
Browser Output
array(3) {
  ["name"]=>
  string(8) "John Doe"
  ["age"]=>
  int(22)
  ["email"]=>
  string(16) "[email protected]"
}

Explanation:

  1. Both arrays have the β€˜name’ and β€˜age’ keys, for this, the elements of the left-hand array are used, and the elements of the right-hand array are ignored.
  2. But, the ’email’ element only exists in the right-hand array, for this, the ’email’ element is added to the left-hand array.

== Equality Operator Example:

The Equality(==) Operator is used to compare two arrays. It will return TRUE if the first and second array have the same key/value pairs (Orders do not matter)

<?php
  $array_1 = array('name' => 'John Doe', 'age' => 22);
  $array_2 = array('name' => 'John Doe', 'age' => 22);

  if($array_1 == $array_2){
      echo '1 - True';
  }
  else{
      echo '1 - False';
  }

  echo "<br>";
  //--------

  $array_1 = array('name' => 'John Doe', 'age' => 22);
  $array_2 = array('age' => 22, 'name' => 'John Doe');

  //Orders do not matter
  if($array_1 == $array_2){
      echo '2 - True';
  }
  else{
      echo '2 - False';
  }

  echo "<br>";
  //--------

  $array_1 = array('name' => 'John Doe', 'age' => 22);
  $array_2 = array('name' => 'John Doe', 'age' => '22');

  //Same Data Types are not mandatory
  if($array_1 == $array_2){
      echo '3 - True';
  }
  else{
      echo '3 - False';
  }

  echo "<br>";
  //--------

  $array_1 = array('name' => 'John Doe', 'age' => 22);
  $array_2 = array('name' => 'Mark', 'age' => 35 );

  if($array_1 == $array_2){
      echo '4 - True';
  }
  else{
      echo '4 - False';
  }

  echo "<br>";
  //--------

  $array_1 = array('name' => 'John Doe', 'age' => 22);
  $array_2 = array('name' => 'John Doe', 'age' => 22, 'email' => '[email protected]');

  if($array_1 == $array_2){
      echo '5 - True';
  }
  else{
      echo '5 - False';
  }
?>
Browser Output
1 – True
2 – True
3 – True
4 – False
5 – False

=== Identity Operator Example:

Identity Operator returns true if the first array and the second array have the same key/value pairs in the same order and of the same types.

<?php
  $array_1 = array('name' => 'John Doe', 'age' => 22);
  $array_2 = array('name' => 'John Doe', 'age' => 22);

  if($array_1 === $array_2){
      echo '1 - True';
  }
  else{
      echo '1 - False';
  }

  echo "<br>";
  //-------------

  $array_1 = array('name' => 'John Doe', 'age' => 22);
  $array_2 = array('age' => 22, 'name' => 'John Doe');

  //The same order is mandatory
  if($array_1 === $array_2){
      echo '2 - True';
  }
  else{
      echo '2 - False';
  }

  echo "<br>";
  //-------------

  $array_1 = array('name' => 'John Doe', 'age' => 22);
  $array_2 = array('name' => 'Mark', 'age' => 35 );

  if($array_1 === $array_2){
      echo '3 - True';
  }
  else{
      echo '3 - False';
  }

  echo "<br>";
  //-------------

  $array_1 = array('name' => 'John Doe', 'age' => 22);
  $array_2 = array('name' => 'John Doe', 'age' => 22, 'email' => '[email protected]');

  if($array_1 === $array_2){
      echo '4 - True';
  }
  else{
      echo '4 - False';
  }

  echo "<br>";
  //-------------

  $array_1 = array('name' => 'John Doe', 'age' => 22);
  $array_2 = array('name' => 'John Doe', 'age' => '22');

  //The same data types are mandatory
  if($array_1 === $array_2){
      echo '5 - True';
  }
  else{
      echo '5 - False';
  }
?>
Browser Output
1 – True
2 – False
3 – False
4 – False
5 – False

!= & <> Inequality Operators Example:

Both Inequality Operators work the same thing. These will return true if the first array is not equal to the second array. (Same order and the same data types are not matter)

<?php
  $array_1 = array('name' => 'John Doe', 'age' => 22);
  $array_2 = array('name' => 'John Doe', 'age' => 22);

  //if($array_1 <> $array_2) - you can also do this
  if($array_1 != $array_2){
      echo '1 - True';
  }
  else{
      echo '1 - False';
  }

  echo "<br>";
  //--------

  $array_1 = array('name' => 'John Doe', 'age' => 22);
  $array_2 = array('age' => 22, 'name' => 'John Doe');

  //Orders do not matter
  if($array_1 != $array_2){
      echo '2 - True';
  }
  else{
      echo '2 - False';
  }

  echo "<br>";
  //--------

  $array_1 = array('name' => 'John Doe', 'age' => 22);
  $array_2 = array('name' => 'John Doe', 'age' => '22');

  //Same Data Types are not mandatory
  if($array_1 != $array_2){
      echo '3 - True';
  }
  else{
      echo '3 - False';
  }

  echo "<br>";
  //--------

  $array_1 = array('name' => 'John Doe', 'age' => 22);
  $array_2 = array('name' => 'Mark', 'age' => 35 );

  if($array_1 != $array_2){
      echo '4 - True';
  }
  else{
      echo '4 - False';
  }

  echo "<br>";
  //--------

  $array_1 = array('name' => 'John Doe', 'age' => 22);
  $array_2 = array('name' => 'John Doe', 'age' => 22, 'email' => '[email protected]');

  if($array_1 != $array_2){
      echo '5 - True';
  }
  else{
      echo '5 - False';
  }
?>
Browser Output
1 – False
2 – False
3 – False
4 – True
5 – True

!== Non-identity Operator Example:

The Non-Identity Operator returns true if the first array is not equal to the second array. But, Both array should have the same key/value pairs in the same order and of the same data types otherwise, it will return false.

<?php
  $array_1 = array('name' => 'John Doe', 'age' => 22);
  $array_2 = array('name' => 'John Doe', 'age' => 22);

  // False Result
  if($array_1 !== $array_2){
      echo '1 - True';
  }
  else{
      echo '1 - False';
  }

  echo "<br>";
  //--------

  $array_1 = array('name' => 'John Doe', 'age' => 22);
  $array_2 = array('age' => 22, 'name' => 'John Doe');

  //The same order is mandatory
  // True Result
  if($array_1 !== $array_2){
      echo '2 - True';
  }
  else{
      echo '2 - False';
  }

  echo "<br>";
  //--------

  $array_1 = array('name' => 'John Doe', 'age' => 22);
  $array_2 = array('name' => 'John Doe', 'age' => '22');

  //Same Data Types are mandatory
  // True Result
  if($array_1 !== $array_2){
      echo '3 - True';
  }
  else{
      echo '3 - False';
  }

  echo "<br>";
  //--------

  //Both array should have the same key/value pairs
  // True Result
  $array_1 = array('name' => 'John Doe', 'age' => 22);
  $array_2 = array('name' => 'Mark', 'age' => 35 );

  if($array_1 !== $array_2){
      echo '4 - True';
  }
  else{
      echo '4 - False';
  }

  echo "<br>";
  //--------

  $array_1 = array('name' => 'John Doe', 'age' => 22);
  $array_2 = array('name' => 'John Doe', 'age' => 22, 'email' => '[email protected]');

  // True Result
  if($array_1 !== $array_2){
      echo '5 - True';
  }
  else{
      echo '5 - False';
  }
?>
Browser Output
1 – False
2 – True
3 – True
4 – True
5 – True