Forum Moderators: coopster

Message Too Old, No Replies

Need help putting data into array / removing duplicates

Preparing a string for a mysql statement

         

Clark

7:35 pm on Jan 16, 2005 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



I've got a variable that looks like:


31, 1, 1,1,1,1,164, 164,29, 29,29,29,37,2,2,2,2,2,2,
147,147,26,26,49, 138, 138,138,138,138,138,138,137, 137,168,
170, 170,48, 172, 44, 187, 183, 185, 195, 184, 186, 45, 46,
194, 167, 182, 181, 34, 18, 16, 25, 28, 53, 53,11, 11,10, 20,
192, 39, 54,54,54,54,54,54,54,54,54,54,54,54,54,54,54,54,54,54,
54,54,54,54,54,54,54,54,54,54,54,54,54,54,54,54,54,54,54,54,
54,54,54,54,54,54,54,54,54,54,54,54,54,54,54,54,54,54,54,54,
54,54,54,54,54,58, 58,58,58,58,58,58,58,58,58,58,58,58,58,58,
58,58, 175, 176, 180, 177, 178, 179,

I'm sending it to a mysql query which will say NOT IN($variable 0) (the 0 is added because the variable ends with a comma). I'd like to remove the duplicates but have no idea how. I figured if I stick it in an array it will be easier. There is an array_unique function after all. But I have no idea how to get the data in an array. I tried $arr1 = str_split($variable); but it spit out an error, probably due to the trailing comma.

Any ideas?

[edited by: ergophobe at 5:50 pm (utc) on Jan. 17, 2005]
[edit reason] fixed sidescroll [/edit]

demnetia

9:37 pm on Jan 16, 2005 (gmt 0)

10+ Year Member



$var = implode(',', array_unique(preg_split('#\s*,\s*#', $var, -1, PREG_SPLIT_NO_EMPTY)));

This way, you won't even need that extra 0 in the SQL query.

Clark

7:17 am on Jan 17, 2005 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



EDIT: After doing it correctly, the code worked perfectly. Thank you.