O'Reilly logo

Core PHP Programming, Third Edition by Leon Atkinson

Stay ahead with the world's most comprehensive technology and business learning platform.

With Safari, you learn the way you learn best. Get unlimited access to videos, live online training, learning paths, books, tutorials, and more.

Start Free Trial

No credit card required

5.6. The + Operator

The + operator has a special meaning for arrays. It merges the elements from the array on the right into the array on the left. The keys of the arrays are important. If a key exists in the array on the left, it remains unchanged. Only elements from the array on the right with different keys merge into the array on the left. Listing 5.8 demonstrates this functionality.

Listing 5.8. Using the + with arrays
<?php
    //define a couple of arrays
    $a = array(
        0=>"Apple",
        2=>"Ball");
    $b = array(
        1=>"Cat",
        2=>"Dog");

    foreach(($a + $b) as $key=>$value)
    {
        print("$key: $value<br>\n");
    }
?>

Figure 5.1 shows that Listing 5.8 prints an array with three elements. The element indexed by 2 uses the value from a, not b.

Chapter 11 discusses ...

With Safari, you learn the way you learn best. Get unlimited access to videos, live online training, learning paths, books, interactive tutorials, and more.

Start Free Trial

No credit card required