Is there anyway to find duplicates in array values? like:-
$cars = array("Volvo", "BMW", "Toyota", "BMW", "Toyota");
As BMW and Toyota occurs twice output would be BMW and Toyota i know about array_search() but in that you have to provide what you want to search.. i can match array value with respect to key but size of array can vary, It would be great if anyone help me out.
One option is using array_count_values()
and include only array elements with more than one values.
$cars = array("Volvo", "BMW", "Toyota", "BMW", "Toyota");
foreach( array_count_values($cars) as $key => $val ) {
if ( $val > 1 ) $result[] = $key; //Push the key to the array sice the value is more than 1
}
echo "<pre>";
print_r( $result );
echo "</pre>";
This will result to:
Array
(
[0] => BMW
[1] => Toyota
)
Doc: array_count_values()
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With