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
array_map
appliesstrtolower
to all values and returns an array with all values lower cased,array_unique
removes all duplicates from return ofarray_map
array_intersect_key
returns an array containing all the entries of $array which have keys that are present in return ofarray_unique
- 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.phparray_intersect_key
: http://php.net/manual/en/function.array-intersect-key.phpstrtolower
: 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
Select Within 20 Kilometers Based on Latitude/Longitude
PHP Curl Get Request and Request's Body
Php: Limit Foreach() Statement
Codeigniter Model Error: Undefined Property
Wordpress Plugin -> Call to Undefined Function Wp_Get_Current_User()
Using If(!Empty) with Multiple Variables Not in an Array
What Does the Dot-Slash Do to PHP Include Calls
Laravel and View Caching in Development -- Can't See Changes Right Away
Upload Images on Twitter Using PHP
PHP Directory List from Remote Server
Uncompress a Gzip File from Curl, on PHP
Using SQLite3 in PHP How to Count the Number of Rows in a Result Set
How to Round to Nearest Thousand
How to Show Ajax Loading Gif Animation While the Page Is Loading