PHP Array Operator


 

PHP Array Operator

In this tutorial we will study about array operators, array operator '+' is used to get union of arrays and the rest of the operators like '==','===' works same they do on the variables. Examples in this tutorial will make it more clear.

In this tutorial we will study about array operators, array operator '+' is used to get union of arrays and the rest of the operators like '==','===' works same they do on the variables. Examples in this tutorial will make it more clear.

Array Operators:

In PHP there are many operators are available which works on more than one array, to get union of two arrays, to check the identity, equality etc.

'+' operator is used to get the union of two arrays, but if the keys of both the array are same then the value of first array will not be overridden and the value remain unchanged.

The functionality of other operators are same like '= =' checks the equality, '= = =' checks the identity, '!='/ '<>' checks the inequality, '!= =' checks the non-identity of two array.

Example:

<?php

$a=array("J"=>"Java","P"=>"PHP","F"=>"Flex");

$b=array("j"=>"JSP","P"=>"PHP","A"=>"ASP");

$c=array("S"=>"JSP","H"=>"PHP","P"=>"ASP");

$d=array("J"=>"Java","P"=>"PHP","F"=>"Flex");

echo "Values of array a are:<br/>";

print_r ($a);

echo "<br/>Values of array b are:<br/>";

print_r ($b);

echo "<br/>Values of array c are:<br/>";

print_r ($c);

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

echo "Union of arrays a and b: ";

print_r ($a+$b);

echo"<br/>";

echo "Union of arrays b and c: ";

print_r($c+$b);

echo "<br/>";

echo "Equality of arrays a and b: ";

var_dump($a==$b);

echo"<br/>";

echo "Equality of arrays c and b: ";

var_dump($c==$b);

0

echo "<br/>";

echo "Equality of arrays a and d: ";

var_dump($a==$d);

1

echo "<br/>";

echo "Identity of arrays a and b: ";

var_dump($a===$b);

2

echo "<br/>";

echo "Identity of arrays c and b: ";

var_dump($c===$b);

3

echo"<br/>";

echo "Identity of arrays a and d: ";

var_dump($a===$d);

4

echo"<br/>";

echo "Inequality of arrays a and b: ";

5

var_dump($a!=$b);

echo"<br/>";

echo "Inequality of arrays c and b: ";

6

var_dump($c<>$b);

echo "<br/>";

echo "Inequality of arrays a and d: ";

7

var_dump($a!=$d);

echo "<br/>";

echo "Non-Identity of arrays a and b: ";

8

var_dump($a!==$b);

echo "<br/>";

echo "Non-Identity of arrays c and b: ";

9

var_dump($c!==$b);

echo"<br/>";

echo "Non-Identity of arrays a and d: ";

0

var_dump($a!==$d);

?>

1

 

Output:

Values of array a are:
Array ( [J] => Java [P] => PHP [F] => Flex )
Values of array b are:
Array ( [j] => JSP [P] => PHP [A] => ASP )
Values of array c are:
Array ( [S] => JSP [H] => PHP [P] => ASP )

Union of arrays a and b: Array ( [J] => Java [P] => PHP [F] => Flex [j] => JSP [A] => ASP )
Union of arrays b and c: Array ( [S] => JSP [H] => PHP [P] => ASP [j] => JSP [A] => ASP )
Equality of arrays a and b: bool(false)
Equality of arrays c and b: bool(false)
Equality of arrays a and d: bool(true)
Identity of arrays a and b: bool(false)
Identity of arrays c and b: bool(false)
Identity of arrays a and d: bool(true)
Inequality of arrays a and b: bool(true)
Inequality of arrays c and b: bool(true)
Inequality of arrays a and d: bool(false)
Non-Identity of arrays a and b: bool(true)
Non-Identity of arrays c and b: bool(true)
Non-Identity of arrays a and d: bool(false)

2

Ads