array_unique

PHP programming forum. Ask questions or help people concerning PHP code. Don't understand a function? Need help implementing a class? Don't understand a class? Here is where to ask. Remember to do your homework!

Moderator: General Moderators

Post Reply
swetha
Forum Commoner
Posts: 88
Joined: Wed Sep 10, 2008 11:00 pm

array_unique

Post 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
User avatar
requinix
Spammer :|
Posts: 6617
Joined: Wed Oct 15, 2008 2:35 am
Location: WA, USA

Re: array_unique

Post by requinix »

Are you testing this on a Windows machine?
swetha
Forum Commoner
Posts: 88
Joined: Wed Sep 10, 2008 11:00 pm

Re: array_unique

Post by swetha »

yes
User avatar
requinix
Spammer :|
Posts: 6617
Joined: Wed Oct 15, 2008 2:35 am
Location: WA, USA

Re: array_unique

Post by requinix »

Instead of explode() try

Code: Select all

$str=preg_split('/\r\n?|\n/',$str);
swetha
Forum Commoner
Posts: 88
Joined: Wed Sep 10, 2008 11:00 pm

Re: array_unique

Post by swetha »

i still get the same "incorrect" output after giving

Code: Select all

 
$str=preg_split('/\r\n?|\n/',$str);
 
User avatar
pickle
Briney Mod
Posts: 6445
Joined: Mon Jan 19, 2004 6:11 pm
Location: 53.01N x 112.48W
Contact:

Re: array_unique

Post by pickle »

Some extra spaces/whitespace maybe?
Real programmers don't comment their code. If it was hard to write, it should be hard to understand.
swetha
Forum Commoner
Posts: 88
Joined: Wed Sep 10, 2008 11:00 pm

Re: array_unique

Post 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
User avatar
requinix
Spammer :|
Posts: 6617
Joined: Wed Oct 15, 2008 2:35 am
Location: WA, USA

Re: array_unique

Post 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));
swetha
Forum Commoner
Posts: 88
Joined: Wed Sep 10, 2008 11:00 pm

Re: array_unique

Post 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?
Post Reply