www.pickatutorial.com Computer Tutorials
Top eBooks: C/C++ | C# | Android | Mathematics | Database | Cloud | Graphics | Networking | Oracle | Hardware | AI
Top Tutorials: C/C++ | C#.NET | PHP MySQL | Java | Java Script | jQuery | HTML | xHTML | HTML5 | VB Script| CSS
Lessons PHP Array Operators Bookmark and Share
Lesson 1
Lesson 2
Lesson 3
Lesson 4
Lesson 5
Lesson 6
Lesson 7
Lesson 8
Lesson 9
Lesson 10
Lesson 11
Lesson 12
Lesson 13
Lesson 14
Lesson 15
Lesson 16
Lesson 17
Lesson 18
Lesson 19
Lesson 20
Lesson 21
Lesson 22
Lesson 23
Lesson 24
Lesson 25
Lesson 26
Lesson 27
Lesson 28
Lesson 29
Lesson 30
Lesson 31
Lesson 32
Lesson 33
Lesson 34
Lesson 35
Lesson 36
Lesson 37
Lesson 38
Lesson 39
Lesson 40
Lesson 41
Lesson 42
Lesson 43
Lesson 44
There are a number of useful operations that we can apply on arrays in PHP.

Union of Arrays
We can form a union of two or more arrays by using the + operator, as shown below:

$a = array("a" => "mango", "b" => "grapes");
$b = array("c" => "pear", "d" => "banana", "e" => "apple");
$c = $a + $b // combines the two arrays

The above code declares two arrays $a and $b and then combines both of them. This combined array is stored in $c. The newly created $c array has all the items from $a and $b i.e.:

$c["a"] refers to "mango",
$c["b"] refers "grapes"),
$c["c"] refers to pear,
$c["d"] refers to "banana" and
$c["e"] refers to "apple"


In case there is a conflict in the index values of both the arrays, the elements of first array i.e. the array referred to by the left hand side of the + operator are taken and the elements of the second array are ignored. This is illustrated in the following example:

$a = array("a" => "mango", "b" => "grapes");
$b = array("a" => "pear", "b" => "banana", "c" => "apple");
$c = $a + $b // combines the two arrays

In the above example the array referred to by the $c has the following elements:

$c["a"] refers to "mango",
$c["b"] refers "grapes"),
$c["c"] refers to "apple"

Notice that the elements of $b having the similar key values are ignored.

Checking the Equality 1
We can check the equality of two arrays by using the equality operator (==). It returns boolean true if both arrays have the same key / value pairs and false otherwise. Note that the order of key / value pairs does not matter.

$a = array("a" => "mango", "b" => "grapes");
$b = array("b" => "grapes", "a" => "mango");
if($a == $b) //returns true

Checking the Equality 2
There is another array operator called the identity operator (===), which we can use to check the equality of two arrays with respect to the order of the key / value pairs. It returns boolean true if both arrays have the same key / value pairs and in the same order, and false otherwise.

$a = array("a" => "mango", "b" => "grapes");
$b = array("b" => "grapes", "a" => "mango");
if($a === $b) //returns false

Even though the elements in the arrays $a and $b have the same key / value pairs, the if statement returns false, because the order of the key / value pairs in both the arrays, above, is not the same.

Checking the Inequality
We can check the inequality of two arrays by using != or <>.

$a = array("a" => "mango", "b" => "grapes");
$b = array("b" => "grapes", "a" => "mango");
if( $a != $b) //returns false...both arrays are equal

if( $a <> $b) //returns false...both arrays are equal

Sometimes we need to check the non-identity of two arrays. Consider the following example:

$a = array("a" => "mango", "b" => "grapes");
$b = array("b" => "grapes", "a" => "mango");
if( $a !== $b)
//returns false...both arrays a equal, but not identical i.e. do not have the same order of key / value pairs.



Home - Advertise - Contact - Disclaimer - About Us
© Since 2006 pickatutorial.com -- All Rights Reserved.