-2

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.

mickmackusa
  • 43,625
  • 12
  • 83
  • 136
  • did you try using `SORT_REGULAR` parameter inside `array_unique()` function? – Osama Ibrahim Nov 02 '18 at 12:12
  • 2
    And if you look closely at the `var_dump()` results, the length of your so-called similar values in different. hence they are not the same. – Osama Ibrahim Nov 02 '18 at 12:15
  • 1
    There is missing information there. Those 4 strings look the same, but there are obviously some unprintable characters in them that are not making it to your question. If I check those strings, I get `string(40) "läs våra leveransvillkor/reservationer"`, which means I'm not checking the same information than you are. – yivi Nov 02 '18 at 12:29
  • You are right guys.There where an extra white space which i didnot paid attention to it.No i solved it after i trim every string in side the array . Thank you – Amjad Khalil Nov 02 '18 at 12:50
  • There is no visible whitespace on the left or right sides of the data that you posted in the question. – mickmackusa Nov 02 '18 at 13:00
  • @mickmackusa yes you are right because it is not the same text on real array – Amjad Khalil Nov 02 '18 at 13:02

2 Answers2

2

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)));
mickmackusa
  • 43,625
  • 12
  • 83
  • 136
  • There where an extra white space which i didnot paid attention to it.No i solved it after i trim every string in side the array . Thank you – Amjad Khalil Nov 02 '18 at 12:52
-2

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);

DEMO

Dave
  • 3,073
  • 7
  • 20
  • 33
  • 1
    Same to you: don't you think that `string(62) string(61) string(60)` in OPs question mean that these are different values? – u_mulder Nov 02 '18 at 12:16
  • There where an extra white space which i didnot paid attention to it.No i solved it after i trim every string in side the array . Thank you – Amjad Khalil Nov 02 '18 at 12:52