Jump to content

Function to check for multiple instances of a string in an array?


kernelgpf

Recommended Posts

"Same string" meaning the entirety of each value, not a partial match?

 

<pre>
<?php

function array_has_dupes (&$array) {
	$tmp = array_unique($array);
	return count($array) == count($tmp) ? 0 : 1 ;
}

$pure_array = array('a', 'b', 'c', 1, 2, 3);
$dupe_array = array('a', 'b', 'c', 1, 2, 'a');

echo array_has_dupes($pure_array) ? 'Y' : 'N' ;
echo '<br>';
echo array_has_dupes($dupe_array) ? 'Y' : 'N' ;

?>
</pre>

Archived

This topic is now archived and is closed to further replies.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.