5.6.2 Stripping out duplicate values: array_unique()This is NOT the latest copy of this book; click here for the latest version.
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.
|
Want to see this stuff in print? PHP in a Nutshell takes the core topics covered here, adds in thousands of edits from the editorial team and myself, and combines them to make an unbeatable reference for PHP programmers at all levels.
My latest book has hundreds more tips on how to use PHP, Apache, and MySQL, plus Perl, Python, shell scripts, performance tuning, and more!
|