Case-Insensitive Array_Unique

case-insensitive array_unique

function array_iunique( $array ) {
return array_intersect_key(
$array,
array_unique( array_map( "strtolower", $array ) )
);
}

PHP array unique values despite of upper/lowercase

Try this..

<?php
$input = array("APPLE", "berry", "apple", "berry");

$data = array_intersect_key($input, array_unique(array_map('strtolower', $input)));

print_r($data);
?>

https://eval.in/416659

Best solution to remove duplicate values from case-insensitive array

Would this work?

$r = array_intersect_key($input, array_unique(array_map('strtolower', $input)));

Doesn't care about the specific case to keep but does the job, you can also try to call asort($input); before the intersect to keep the capitalized values instead (demo at IDEOne.com).

Remove duplicate words (case-insensitive)

$text = "Wyse Dell WYSE";

$words = array_iunique(explode(' ',$text));
$words = implode(' ',$words);

echo $words;

function array_iunique($array) {
return array_intersect_key(
$array,
array_unique(array_map("StrToLower",$array))
);
}

source: case-insensitive array_unique

  1. array_map applies strtolower to all values and returns an array with all values lower cased,
  2. array_unique removes all duplicates from return of array_map
  3. array_intersect_key returns an array containing all the entries of $array which have keys that are present in return of array_unique
  4. This will prevent only first existence of word and remove others

More details about functions:

array_map: http://php.net/manual/en/function.array-map.php

array_intersect_key: http://php.net/manual/en/function.array-intersect-key.php

strtolower: http://php.net/strtolower

PHP array_ merge array_unique lower/upper case

First hit on google is the PHP.net page which offers:

function in_iarray($str, $a){
foreach($a as $v){
if(strcasecmp($str, $v)==0){return true;}
}
return false;
}

function array_iunique($a){
$n = array();
foreach($a as $k=>$v){
if(!in_iarray($v, $n)){$n[$k]=$v;}
}
return $n;
}

$input = array("aAa","bBb","cCc","AaA","ccC","ccc","CCC","bBB","AAA","XXX");
$result = array_iunique($input);
print_r($result);

/*
Array
(
[0] => aAa
[1] => bBb
[2] => cCc
[9] => XXX
)
*/

removing (case insensitively) duplicates from array while trimming whitespace

just like this:

$input = array('test ', 'test', ' test2', 'test2');
$r = array_intersect_key($input, array_unique(array_map('strtolower', array_map('trim',$input))));

Fiddle

If you need to trim the results also, you should apply array map on the results too

$r = array_map('trim',array_intersect_key($input, array_unique(array_map('strtolower', array_map('trim',$input)))));

How to remove duplicates from PHP multidimensional array, case insensitive but preserving case?

Rather than manipulating the content of the array, this creates a key to the array (in a similar manner to how you manipulate the main array) and then when you combine the key with the data (using array_combine()) the duplicates are removed (as only 1 key can exist in the result)...

$arr  = [
['id' => 1, 'term' => 'Hello'],
['id' => 1, 'term' => 'hello'],
['id' => 1, 'term' => 'Hello'],
['id' => 2, 'term' => 'Hello']
];

$key = array_map("serialize", $arr);
$key = array_map("strtolower", $key);
$new = array_combine($key, $arr);

print_r(array_values($new));

gives...

Array
(
[0] => Array
(
[id] => 1
[term] => Hello
)

[1] => Array
(
[id] => 2
[term] => Hello
)

)

For the vertically challenged, it can be wrapped into 1 (although less readable) line ...

$new = array_values(array_combine(array_map("strtolower", array_map("serialize", $arr)), $arr));

Case insensitive unique multidimensional array loop

I figured things out in the end thanks to Rasclatt and Haotian Liu. I thought I should put it up just in case people were curious.
Thanks guys!

I changed the array a bit, this is how it looked:

Array
(
[0] => Array
(
[contact_description] => Employment support
[contact_type] => Phone
[contact] => 0300 456 8110
)

[1] => Array
(
[contact_description] => General enquiries
[contact_type] => Phone
[contact] => 0300 456 8052
)

[2] => Array
(
[contact_description] => employment support
[contact_type] => Email
[contact] => employmentservices.osc@remploy.co.u
)

[3] => Array
(
[contact_description] => general enquiries
[contact_type] => Email
[contact] => info@remploy.co.uk
)

)

$res = array();
foreach ($shop as $each) {
$lcValue = strtolower($each['Title']);
if (isset($res[$lcValue]))
array_push($res[$lcValue], $each['contact']);
else
$res[$lcValue] = array($each['contact']);
}

foreach ($res as $name => $contact) {
echo '<h5 class="mb-0">' . ucwords($name) . '</h5>';
foreach ($contact as $contact) {
if (1 === preg_match('~[0-9]~', $contact)) {
// Phone Number
echo '<li class="work_number"><a href="tel:' . $contact . '">' . $contact . '</a></li>';
} elseif (strpos($contact, '@') !== false) {
//Email
echo '<li class="email"><a href="mailto:' . $contact . '" target="_blank">' . $contact . '</a></li>';
} else {
echo '<li><a>' . $contact . '</a></li>';
}
}

}


Related Topics



Leave a reply



Submit