Home » PHP » PHP check duplicate values in an array and return it

PHP check duplicate values in an array and return it

How to check duplicate values in an array and return it using PHP?Also telling php check wild card value in an array and return those values.For a example you want to check number of times that values exist and return that all values in an array.Check this example, you can understand this better.

// This is the array
$Keywords = array("One","three","four","one","two", "one");

//convert to lowercase
$Keywords = array_map('strtolower', $Keywords);

// query for search the value
$char = trim($_GET['q']);

if(ctype_upper($char)) // if first character uppercase,
{
$char = strtolower($_GET['q']); // then convert to lowercase
}

//Checking wild card values
function in_array_wildcard( $needle, $arr )
{
return array_values( preg_grep( '/' . str_replace( '*', '.*', $needle ) . '/', $arr ) );
}

// call to the function
$duplicateArr = in_array_wildcard( $char, $Keywords );
print_r($duplicateArr);
{/code}

Tags

Similar posts