jaay Posted April 13, 2010 Share Posted April 13, 2010 I have certain fields in DB table that are actually comma separated words like mango,orange,apple. Now I make an array with rows from the table like mango,orange,apple and peach,cherry,apple etc. Now if I make an array with this and explode to find comma delimited value it looks like, mango orange apple peach cherry apple. Now as you can see apple appears twice. I want it to appear only once. I used array_unique but that doesnt seem to work. How to do this. Please help? Quote Link to comment https://forums.phpfreaks.com/topic/198447-array_unique/ Share on other sites More sharing options...
teamatomic Posted April 13, 2010 Share Posted April 13, 2010 array_unique($array) wont work, $new_array=array_unique($array) will work. HTH Teamatomic Quote Link to comment https://forums.phpfreaks.com/topic/198447-array_unique/#findComment-1041315 Share on other sites More sharing options...
jaay Posted April 13, 2010 Author Share Posted April 13, 2010 Hmm. that's what I tried .. I put the array array1 with the array_unique function like $new_array=array_unique($array1); but it doesn't have any effect... Quote Link to comment https://forums.phpfreaks.com/topic/198447-array_unique/#findComment-1041318 Share on other sites More sharing options...
teamatomic Posted April 13, 2010 Share Posted April 13, 2010 Are you sure $array1 is actually an array and not a string? Do a var_dump on it and show the result. HTH Teamatomic Quote Link to comment https://forums.phpfreaks.com/topic/198447-array_unique/#findComment-1041324 Share on other sites More sharing options...
jaay Posted April 13, 2010 Author Share Posted April 13, 2010 here is the echo from the var_dump ..... array(4) { [0]=> string(4) "beer" [1]=> string( " alcohol" [2]=> string(6) " apple" [3]=> string(7) " cherry" } array(4) { [0]=> string(4) "beer" [1]=> string( " alcohol" [2]=> string(6) " apple" [3]=> string(7) " cherry" } array(4) { [0]=> string(4) "beer" [1]=> string( " alcohol" [2]=> string(6) " apple" [3]=> string(7) " cherry" } array(4) { [0]=> string(4) "beer" [1]=> string( " alcohol" [2]=> string(6) " apple" [3]=> string(7) " cherry" } array(4) { [0]=> string(5) "ac-dc" [1]=> string(5) " rock" [2]=> string(6) " music" [3]=> string(4) "band" } array(4) { [0]=> string(5) "ac-dc" [1]=> string(5) " rock" [2]=> string(6) " music" [3]=> string(4) "band" } array(4) { [0]=> string(5) "ac-dc" [1]=> string(5) " rock" [2]=> string(6) " music" [3]=> string(4) "band" } array(4) { [0]=> string(5) "ac-dc" [1]=> string(5) " rock" [2]=> string(6) " music" [3]=> string(4) "band" } array(4) { [0]=> string(4) "rock" [1]=> string(6) " music" [2]=> string(2) "30" [3]=> string(11) " television" } array(4) { [0]=> string(4) "rock" [1]=> string(6) " music" [2]=> string(2) "30" [3]=> string(11) " television" } array(4) { [0]=> string(4) "rock" [1]=> string(6) " music" [2]=> string(2) "30" [3]=> string(11) " television" } array(4) { [0]=> string(4) "rock" [1]=> string(6) " music" [2]=> string(2) "30" [3]=> string(11) " television" } array(3) { [0]=> string(5) "apple" [1]=> string(5) " beer" [2]=> string(6) " mango" } array(3) { [0]=> string(5) "apple" [1]=> string(5) " beer" [2]=> string(6) " mango" } array(3) { [0]=> string(5) "apple" [1]=> string(5) " beer" [2]=> string(6) " mango" } array(7) { [0]=> string(4) "beer" [1]=> string(6) " belly" [2]=> string( " alcohol" [3]=> string(6) " drunk" [4]=> string(6) " funny" [5]=> string(4) " sex" [6]=> string( " machine" } array(7) { [0]=> string(4) "beer" [1]=> string(6) " belly" [2]=> string( " alcohol" [3]=> string(6) " drunk" [4]=> string(6) " funny" [5]=> string(4) " sex" [6]=> string( " machine" } array(7) { [0]=> string(4) "beer" [1]=> string(6) " belly" [2]=> string( " alcohol" [3]=> string(6) " drunk" [4]=> string(6) " funny" [5]=> string(4) " sex" [6]=> string( " machine" } array(7) { [0]=> string(4) "beer" [1]=> string(6) " belly" [2]=> string( " alcohol" [3]=> string(6) " drunk" [4]=> string(6) " funny" [5]=> string(4) " sex" [6]=> string( " machine" } array(7) { [0]=> string(4) "beer" [1]=> string(6) " belly" [2]=> string( " alcohol" [3]=> string(6) " drunk" [4]=> string(6) " funny" [5]=> string(4) " sex" [6]=> string( " machine" } array(7) { [0]=> string(4) "beer" [1]=> string(6) " belly" [2]=> string( " alcohol" [3]=> string(6) " drunk" [4]=> string(6) " funny" [5]=> string(4) " sex" [6]=> string( " machine" } array(7) { [0]=> string(4) "beer" [1]=> string(6) " belly" [2]=> string( " alcohol" [3]=> string(6) " drunk" [4]=> string(6) " funny" [5]=> string(4) " sex" [6]=> string( " machine" } array(4) { [0]=> string(7) "beatles" [1]=> string(6) " music" [2]=> string(5) " rock" [3]=> string(12) " john lennon" } array(4) { [0]=> string(7) "beatles" [1]=> string(6) " music" [2]=> string(5) " rock" [3]=> string(12) " john lennon" } array(4) { [0]=> string(7) "beatles" [1]=> string(6) " music" [2]=> string(5) " rock" [3]=> string(12) " john lennon" } array(4) { [0]=> string(7) "beatles" [1]=> string(6) " music" [2]=> string(5) " rock" [3]=> string(12) " john lennon" } array(2) { [0]=> string(4) "beer" [1]=> string(5) " bush" } array(2) { [0]=> string(4) "beer" [1]=> string(5) " bush" } array(2) { [0]=> string(4) "beer" [1]=> string(5) " idol" } array(2) { [0]=> string(4) "beer" [1]=> string(5) " idol" } array(1) { [0]=> string(0) "" } array(3) { [0]=> string(6) "orange" [1]=> string(5) "peach" [2]=> string(7) "bannana" } array(3) { [0]=> string(6) "orange" [1]=> string(5) "peach" [2]=> string(7) "bannana" } array(3) { [0]=> string(6) "orange" [1]=> string(5) "peach" [2]=> string(7) "bannana" } array(2) { [0]=> string(4) "beer" [1]=> string(6) " again" } array(2) { [0]=> string(4) "beer" [1]=> string(6) " again" } array(2) { [0]=> string(4) "moar" [1]=> string(5) " beer" } array(2) { [0]=> string(4) "moar" [1]=> string(5) " beer" } array(1) { [0]=> string(4) "beer" } array(2) { [0]=> string(4) "beer" [1]=> string(6) " irish" } array(2) { [0]=> string(4) "beer" [1]=> string(6) " irish" } array(2) { [0]=> string(4) "beer" [1]=> string(7) " things" } array(2) { [0]=> string(4) "beer" [1]=> string(7) " things" } array(2) { [0]=> string(4) "beer" [1]=> string(2) " w" } array(2) { [0]=> string(4) "beer" [1]=> string(2) " w" } Quote Link to comment https://forums.phpfreaks.com/topic/198447-array_unique/#findComment-1041344 Share on other sites More sharing options...
teamatomic Posted April 14, 2010 Share Posted April 14, 2010 From the PHP manual: Note: Note that array_unique() is not intended to work on multi dimensional arrays. HTH Teamatomic Quote Link to comment https://forums.phpfreaks.com/topic/198447-array_unique/#findComment-1041361 Share on other sites More sharing options...
jaay Posted April 14, 2010 Author Share Posted April 14, 2010 Hmmm. okay. But here's what I think could be done. Can I extract only the required stuffs like the 'apple','mango' strings from this array and make a comma separated array only with these strings and then do a array_unique? I guess that can be done..but I am failry new to php ..so have little clue. Any help regarding this? Quote Link to comment https://forums.phpfreaks.com/topic/198447-array_unique/#findComment-1041998 Share on other sites More sharing options...
newbtophp Posted April 14, 2010 Share Posted April 14, 2010 If you look at the documentation: http://php.net/manual/en/function.array-unique.php Theirs some interesting user notes for multi dimensional array(s). Quote Link to comment https://forums.phpfreaks.com/topic/198447-array_unique/#findComment-1042008 Share on other sites More sharing options...
teamatomic Posted April 14, 2010 Share Posted April 14, 2010 What you propose wont work. But as just suggested the user submitted section of the man page has a solution for multi arrays. HTH Teamatomic Quote Link to comment https://forums.phpfreaks.com/topic/198447-array_unique/#findComment-1042020 Share on other sites More sharing options...
jaay Posted April 14, 2010 Author Share Posted April 14, 2010 yup. The user submitted section was quite helpful. I found the solution. Thanx for pointing me there Quote Link to comment https://forums.phpfreaks.com/topic/198447-array_unique/#findComment-1042032 Share on other sites More sharing options...
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.