Stripping out duplicate values

array array_unique ( array input)

Consider if the $toppings2 array had been declared like this:

$toppings2 = array("Peppers", "Ham", "Cheese", "Peppers");

In this situation, there'd be two Peppers elements in there, in which case we would want to strip out the duplicate Peppers before running the merges and intersects. This is done by using array_unique(), which takes an array as its parameter and returns the same array with its duplicate values removed, like this:

$toppings2 = array_unique($toppings2);

Try the example code again, this time removing the duplicate Cheese values from $bothtoppings.


Next chapter: Filtering your array through a function >>

Previous chapter: Chopping and changing arrays

Jump to:


Home: Table of Contents

Follow us on or Twitter

Username:   Password:
Create Account | About TuxRadar