Page 1 of 1

array_unique

Posted: Wed Oct 29, 2008 1:35 am
by swetha

Code: Select all

 
$str=explode("\n",$str);
print_r($str);
$result=array_unique($str);
print_r($result);
 

I tried the above code and displayed the output.The values are entered in one textarea and the output is displayed in another textarea.The last value "aa" which is getting repeated should not be displayed,but it is getting displayed.
Input:
Array
(
[0] => cc
[1] => cc
[2] => bb
[3] => bb
[4] => aa
[5] => aa
)

Ouput:
Array
(
[0] => cc
[2] => bb
[4] => aa
[5] => aa
)
----------------------------------------------------------------------------
But the actual output needs to be
Array
(
[0] => cc
[2] => bb
[4] => aa
)

i.e $str[5] should not be displayed.kindly help

Re: array_unique

Posted: Wed Oct 29, 2008 2:30 am
by requinix
Are you testing this on a Windows machine?

Re: array_unique

Posted: Wed Oct 29, 2008 2:55 am
by swetha
yes

Re: array_unique

Posted: Wed Oct 29, 2008 3:21 am
by requinix
Instead of explode() try

Code: Select all

$str=preg_split('/\r\n?|\n/',$str);

Re: array_unique

Posted: Wed Oct 29, 2008 5:08 am
by swetha
i still get the same "incorrect" output after giving

Code: Select all

 
$str=preg_split('/\r\n?|\n/',$str);
 

Re: array_unique

Posted: Wed Oct 29, 2008 1:52 pm
by pickle
Some extra spaces/whitespace maybe?

Re: array_unique

Posted: Wed Oct 29, 2008 11:45 pm
by swetha

Code: Select all

 
$str=explode("\n",trim($str));
$result=array_unique($str);
 
i tried the above,still it didnt work.im getting the repeated output for the last value

Input:
aa
aa
bb
bb

Output:
aa
bb
bb

Re: array_unique

Posted: Thu Oct 30, 2008 12:18 am
by requinix
Well, what do you know, pickle was right.

The code is acting correctly because aa is not the same as aa .

Oh, sorry, did you not catch that? Let me try again:

Code: Select all

Well, what do you know.
 
The code is acting correctly because aa is not the same as aa                                                                                                                                             .
So somehow when you were copy/pasting the output from your code you failed to notice the huge number of spaces.

Code: Select all

Input:
Array
(
    [0] => cc
    [1] => cc
    [2] => bb
    [3] => bb
    [4] => aa
    [5] => aa                                                                                                                                             
)

Code: Select all

Ouput:
Array
(
    [0] => cc
    [2] => bb
    [4] => aa
    [5] => aa                                                                                                                                             
)
And then on your latest post you didn't copy/paste like before: I guess this time you typed it.

Code: Select all

$str=preg_split('/\s*(\r\n?|\n)\s*/', trim($str));

Re: array_unique

Posted: Thu Oct 30, 2008 1:03 am
by swetha
can u pls explain the meaning of this regular expression

Code: Select all

 
$str=preg_split('/\s*(\r\n?|\n)\s*/', trim($str))?
 
pls clearly explain why the code which i gave didnt work?