Skip to Content
Core PHP Programming, Third Edition
book

Core PHP Programming, Third Edition

by Leon Atkinson
August 2003
Intermediate to advanced
1104 pages
19h 27m
English
Pearson
Content preview from Core PHP Programming, Third Edition

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 ...

Become an O’Reilly member and get unlimited access to this title plus top books and audiobooks from O’Reilly and nearly 200 top publishers, thousands of courses curated by job role, 150+ live events each month,
and much more.
Start your free trial

You might also like

Programming PHP, 3rd Edition

Programming PHP, 3rd Edition

Rasmus Lerdorf, Kevin Tatroe, Peter MacIntyre
Programming PHP

Programming PHP

Rasmus Lerdorf, Kevin Tatroe
Programming PHP, 2nd Edition

Programming PHP, 2nd Edition

Rasmus Lerdorf, Kevin Tatroe, Peter MacIntyre
Advanced PHP Programming

Advanced PHP Programming

George Schlossnagle

Publisher Resources

ISBN: 0130463469Purchase book