Share on Google+Share on Google+

PHP Array Operator

Advertisement
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);

echo "<br/>";

echo "Equality of arrays a and d: ";

var_dump($a==$d);

echo "<br/>";

echo "Identity of arrays a and b: ";

var_dump($a===$b);

echo "<br/>";

echo "Identity of arrays c and b: ";

var_dump($c===$b);

echo"<br/>";

echo "Identity of arrays a and d: ";

var_dump($a===$d);

echo"<br/>";

echo "Inequality of arrays a and b: ";

var_dump($a!=$b);

echo"<br/>";

echo "Inequality of arrays c and b: ";

var_dump($c<>$b);

echo "<br/>";

echo "Inequality of arrays a and d: ";

var_dump($a!=$d);

echo "<br/>";

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

var_dump($a!==$b);

echo "<br/>";

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

var_dump($c!==$b);

echo"<br/>";

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

var_dump($a!==$d);

?>

 

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)

Advertisements

Advertisement

Posted on: March 20, 2010 If you enjoyed this post then why not add us on Google+? Add us to your Circles

Share this Tutorial Follow us on Twitter, or add us on Facebook or Google Plus to keep you updated with the recent trends of Java and other open source platforms.