Programming PHP Array operators in PHP

Array operators in PHP

array operators in php

To perform operations on arrays easily PHP array operators is a good choice. We can use Union operators, Equality operators, Identity operator, Inequality operators and Non-identity operators to perform operations on arrays in PHP. There are various types of array operators described below.

Union Array Operator (+)

Union operator is denoted by + sign. It append the array on the right side to the array on left. If both arrays have any key in common (key exist in both arrays) then elements of left side array will be considered and matching elements from right side array will be ignored.

Example :

<?php

$arr1 = ['a' => 'Apple', 'p' => 'Pineapple', 'k' => 'Kiwi'];
$arr2 = ['p' => 'Peach', 'o' => 'Orange', 'm' => 'Mango'];


$union1 = $arr1 + $arr2;
echo 'Union of $arr1 and $arr2 is:<br>';

print_r($union1);


$union2 = $arr2 + $arr1;
echo '<br><br>Union of $arr2 and $arr1 is:<br>';

print_r($union2)
?>

Output

Union of $arr1 and $arr2 is:
Array ( [a] => Apple [p] => Pineapple [k] => Kiwi [o] => Orange [m] => Mango ) 

Union of $arr2 and $arr1 is:
Array ( [p] => Peach [o] => Orange [m] => Mango [a] => Apple [k] => Kiwi )

In the above example, key “p” is common so in both operations the value of key “p” on the right side is considered and ignored of the left array.

Equality Array Operator (==)

To check if both the arrays have same elements (key value pairs) or not, Equality operator is used.

If array1 and array2 have the same elements (key value pairs) then boolean true is returned, else boolean false is returned.

Example :

<?php
$arr1 = ['a' => 'Apple', 'p' => 'Pineapple', 'k' => 'Kiwi'];
$arr2 = ['a' => 'Apple', 'p' => 'Pineapple', 'k' => 'Kiwi'];
$arr3 = ['b' => 'Apple', 'p' => 'Pineapple', 'k' => 'Kiwi'];
$arr4 = ['a' => 'Banana', 'p' => 'Pineapple', 'k' => 'Kiwi'];

$check1 = $arr1 == $arr2;
var_dump($check1);
#Result bool(true)

$check2 = $arr1 == $arr3;
var_dump($check2);
#Result bool(false)

$check3 = $arr1 == $arr4;
var_dump($check3);
#Result bool(false)
?>

Inequality Array Operator (!=)

Inequality Array Operator is used to check if both arrays have the same elements or not It returns Boolean true if arrays are not equal, Boolean false for equal arrays.

Example :

<?php
$arr1 = ['a' => 'Apple', 'p' => 'Pineapple', 'k' => 'Kiwi'];
$arr2 = ['a' => 'Apple', 'p' => 'Pineapple', 'k' => 'Kiwi'];
$arr3 = ['b' => 'Apple', 'p' => 'Pineapple', 'k' => 'Kiwi'];
$arr4 = ['a' => 'Banana', 'p' => 'Pineapple', 'k' => 'Kiwi'];

$check1 = $arr1 != $arr2;
var_dump($check1);
#Result bool(false)

$check2 = $arr1 != $arr3;
var_dump($check2);
#Result bool(true)

$check3 = $arr1 != $arr4;
var_dump($check3);
#Result bool(true)
?>

Identity Array Operator (===)

Two arrays are Identical only if the Keys and Values both are equal.

Example :

<?php
$arr1 = ['a' => 'Apple', 'p' => 'Pineapple', 'k' => 'Kiwi'];
$arr2 = ['p' => 'Pineapple', 'a' => 'Apple', 'k' => 'Kiwi'];
$arr3 = ['b' => 'Apple', 'p' => 'Pineapple', 'k' => 'Kiwi'];
$arr4 = ['a' => 'Banana', 'p' => 'Pineapple', 'k' => 'Kiwi'];
$arr5 = ['a' => 'Apple', 'p' => 'Pineapple', 'k' => 'Kiwi'];

$check1 = $arr1 === $arr2;
var_dump($check1);
#Result bool(false)

$check2 = $arr1 === $arr3;
var_dump($check2);
#Result bool(false)

$check3 = $arr1 === $arr4;
var_dump($check3);
#Result bool(false)

$check4 = $arr1 === $arr5;
var_dump($check4);
#Result bool(true)
?>

Non-identity Array Operator (!==)

Two arrays are Non-identical if the Keys and Values both are not equal.

Example :

<?php
$arr1 = ['a' => 'Apple', 'p' => 'Pineapple', 'k' => 'Kiwi'];
$arr2 = ['p' => 'Pineapple', 'a' => 'Apple', 'k' => 'Kiwi'];
$arr3 = ['b' => 'Apple', 'p' => 'Pineapple', 'k' => 'Kiwi'];
$arr4 = ['a' => 'Banana', 'p' => 'Pineapple', 'k' => 'Kiwi'];
$arr5 = ['a' => 'Apple', 'p' => 'Pineapple', 'k' => 'Kiwi'];

$check1 = $arr1 !== $arr2;
var_dump($check1);
#Result bool(true)

$check2 = $arr1 !== $arr3;
var_dump($check2);
#Result bool(true)

$check3 = $arr1 !== $arr4;
var_dump($check3);
#Result bool(true)

$check4 = $arr1 !== $arr5;
var_dump($check4);
#Result bool(false)
?>

Leave a Reply