Here I have this array:
$myArray = array(5) {
[0]=> string(62) " läs våra leveransvillkor/reservationer "
[1]=> string(61) " läs våra leveransvillkor/reservationer "
[2]=> string(60) " läs våra leveransvillkor/reservationer"
[3]=> string(107) "om skorstenen bryter nock, ränndal, bjälke, el, vent etc tillkommer kostnad för vinklar eller avväxling"
[4]=> string(59) "läs våra leveransvillkor/reservationer"
}
Here, four values are the same. So I want to keep only one. I did try to use array_unique
, and even I did try this:
array_map("unserialize", array_unique(array_map("serialize", $myArray)));
But I was not succeed in removing duplicates. I guess the issue is because of the special character.
Your var_dump exposes that you have non-printable characters in your strings.
You will need to prepare your data by removing non-printable characters. If you are in UTF-8, this should do...
$myArray = preg_replace('/[\x00-\x1F\x7F]/u', '', $myArray);
Then you will be able to use:
$myArray = array_unique($myArray);
Or of course, combine them into one line:
$myArray = array_unique(preg_replace('/[\x00-\x1F\x7F]/u', '', $myArray));
If you say there is merely leading and trailing whitespace to mop up, then this will do.
Code: ( Demo )
$myArray = [
" läs våra leveransvillkor/reservationer ",
" läs våra leveransvillkor/reservationer ",
" läs våra leveransvillkor/reservationer",
"om skorstenen bryter nock, ränndal, bjälke, el, vent etc tillkommer kostnad för vinklar eller avväxling",
"läs våra leveransvillkor/reservationer"
];
var_export(array_unique(array_map('trim', $myArray)));
it is work with array_unique
$myArray = array(
'0'=> "läs våra leveransvillkor/reservationer",
'1'=> "läs våra leveransvillkor/reservationer",
'2'=> "läs våra leveransvillkor/reservationer",
'3'=> "om skorstenen bryter nock, ränndal, bjälke, el, vent etc tillkommer kostnad för vinklar eller avväxling" ,
'4'=> "läs våra leveransvillkor/reservationer",
);
$result = array_unique($myArray);
print_r($result);
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.