Want more array power? Check out the array operators:
Most of these have to do with comparing arrays, but the + operator is designed to concatenate arrays. You can see an example in phparrayops.php, Example 3-6, where we put to work not only the + operator but also the == operator, checking to see if two arrays have the same elements (in this case, they don't). Example 3-6. Using array operators, phparrayops.php<HTML> <HEAD> <TITLE> Using array operators </TITLE> </HEAD> <BODY> <H1> Using array operators </H1> <?php $fruits["apples"] = 3839; $fruits["oranges"] = 2289; $vegetables["broccoli"] = 1991; $vegetables["corn"] = 9195; echo "\$fruits: "; print_r($fruits); echo "<BR>"; echo "\$vegetables: "; print_r($vegetables); echo "<BR>"; $produce = $fruits + $vegetables; echo "\$produce: "; print_r($produce); echo "<BR>"; if ($fruits == $vegetables){ echo "\$fruits has the same elements as \$vegetables<BR>"; } else { echo "\$fruits does not have the same elements as \$vegetables<BR>"; } ?> </BODY> </HTML> The results appear in Figure 3-6, where you can see that the + operator did indeed concatenate the arrays we wanted it to, and the == operator did indeed compare the two arrays properly. Figure 3-6. Using array operators. |