Broad Network


Array Operators in PHP

PHP Operators - Part 6

Forward: PHP array has operators, which you have to learn how to use. In this part of the series, we look at the array operators.

By: Chrysanthus Date Published: 10 Aug 2012

Introduction

This is part 6 of my series, PHP Operators. PHP array has operators, which you have to learn how to use. In this part of the series, we look at the array operators.

Note: If you cannot see the code or if you think anything is missing (broken link, image absent), just contact me at forchatrans@yahoo.com. That is, contact me for the slightest problem you have about what you are reading.

The Union Operator
The array union operator is +. If you have two arrays, you can have a third array, which is the union of the two arrays. Read and try the following program:

<?php

    $arrA = array("one"=>"aaa", "two"=>"bbb", "three"=>"ccc");
    $arrB = array("three"=>"DDD", "four"=>"EEE", "five"=>"FFF");

    $arrC = $arrA + $arrB;

    foreach ($arrC as $key => $value)
        {
            echo "$key: $value<br />";
        }

?>

The output (foreach) of the above program is:

one: aaa
two: bbb
three: ccc
four: EEE
five: FFF

The key/value pairs of $arrA is:

one: aaa
two: bbb
three: ccc

The key/value pair of $arrB is:

three: DDD
four: EEE
five: FFF

Note that the key, “three” occurs in both sets. However, the values of the key, “three” are different in both arrays. When keys are duplicated like this, the duplicated keys and their corresponding values in the right hand array of the + operator do not appear in the union array.

The Array Equality Operator
The array equality operator is ==. This equality operator is different from the comparison equality operator. You have to learn the meaning of equality operator with arrays. Two arrays are equal if they have the same key/value pairs. It does not matter the order in which the key/value pairs are arranged in the different arrays. Read and try the following code:

<?php

    $arrA = array("one"=>"aaa", "two"=>"bbb", "three"=>"ccc");
    $arrB = array("two"=>"bbb", "one"=>"aaa", "three"=>"ccc");

    if ($arrA == $arrB)
        {
            echo "The two arrays are equal.";
        }

?>

The above two arrays are described as equal and so the array equality operator returns true. Note that the ordering of the pairs in the two arrays are not the same.

The Array Inequality Operator
The array inequality operator is !=. This inequality operator is different from the comparison inequality operator. You have to learn the meaning of the array inequality operator. Well, it is actually the opposite of the array equality operator. Read and try the following code:

<?php

    $arrA = array("one"=>"aaa", "two"=>"bbb", "three"=>"ccc");
    $arrB = array("four"=>"ddd", "two"=>"bbb", "three"=>"ccc");

    if ($arrA != $arrB)
        {
            echo "The two arrays are not equal.";
        }

?>

If any of the keys are not the same or if any of the values are not the same or if any of the pairs are not the same, then the arrays are not equal.

The <> array operator is a synonym for != on arrays.

The Array Identity Operator
The array identity operator, ===, is different from the comparison identity operator. You have to learn the meaning of the array identity. Two arrays are identical if,

a) they are equal;
b) the ordering of the value pairs are the same;
c) the keys of the two arrays are of the same type and the values of the two arrays are of the same type. i.e. key/value pairs are of the same types.

Read and try the following code:

<?php

    $arrA = array("one"=>"aaa", "two"=>"bbb", "three"=>"ccc");
    $arrB = array("one"=>"aaa", "two"=>"bbb", "three"=>"ccc");

    if ($arrA === $arrB)
        {
            echo "The two arrays are identical.";
        }

?>

In this code, the identity operator returns true.

The Non-Identity Array Operator
The non-identity array operator, !==, does the opposite of what the identity array operator does. If any of the identity rules is broken, then the non-identity operator will return true, indicating that the arrays are not identical. Read and try the following code:

<?php

    $arrA = array("one"=>"aaa", "two"=>"bbb", "three"=>"ccc");
    $arrB = array("three"=>"ccc", "one"=>"aaa", "two"=>"bbb");

    if ($arrA !== $arrB)
        {
            echo "The two arrays are not identical.";
        }

?>

That is all about array operators. We take a break here and continue in the next part of the series.

Chrys

Related Links

Major in Website Design
Web Development Course
HTML Course
CSS Course
ECMAScript Course
NEXT

Comments

Become the Writer's Fan
Send the Writer a Message