woop Posted August 5, 2010 Share Posted August 5, 2010 Need a bit of help with some php and a few kind members helped me in the past. My code is: $query = "SELECT names FROM table WHERE type='$type'"; $result = mysql_query($query) or die(mysql_error()); while($row = mysql_fetch_array($result)) { foreach(unserialize($row['names']) as $key => $value){ echo $value . '<br />'; } } Basically, a field on my table is called names. The information in this field is serialized (so many names are together in a single field, for a single row). I want to be able to search the whole table (where type='$type' - so many rows) for all names and then show unique names only. My code above unserializes the names from each field and then lists them, but doesn't deal with any duplicates (which I need removing). I have looked at functions like "unique_array()", and have tried using it in different places but it isn't doing the job. Any help appreciated. Quote Link to comment https://forums.phpfreaks.com/topic/209918-php-help-unique-unserialize/ Share on other sites More sharing options...
wildteen88 Posted August 5, 2010 Share Posted August 5, 2010 $query = "SELECT names FROM table WHERE type='$type'"; $result = mysql_query($query) or die(mysql_error()); while($row = mysql_fetch_array($result)) { $names = array_unique( unserialize($row['names']) ); foreach($names as $key => $value){ echo $value . '<br />'; } } Quote Link to comment https://forums.phpfreaks.com/topic/209918-php-help-unique-unserialize/#findComment-1095680 Share on other sites More sharing options...
AbraCadaver Posted August 5, 2010 Share Posted August 5, 2010 It depends upon what your arrays look like, but this may work: $names = array(); while($row = mysql_fetch_array($result)) { $names = array_unique(array_merge($names, unserialize($row['names']))); } foreach($names as $value){ echo $value . '<br />'; } Quote Link to comment https://forums.phpfreaks.com/topic/209918-php-help-unique-unserialize/#findComment-1095681 Share on other sites More sharing options...
woop Posted August 5, 2010 Author Share Posted August 5, 2010 Thank you both - now working. @wildteen88 Appreciate you trying to help. I'd been working along a similar line as your answer and for some reason the duplicates were left in there. Thanks for your help. @AbraCadaver That did it! You have just saved me a lot of time, effort and headaches. Many thanks. Quote Link to comment https://forums.phpfreaks.com/topic/209918-php-help-unique-unserialize/#findComment-1095689 Share on other sites More sharing options...
AbraCadaver Posted August 5, 2010 Share Posted August 5, 2010 Thank you both - now working. @wildteen88 Appreciate you trying to help. I'd been working along a similar line as your answer and for some reason the duplicates were left in there. Thanks for your help. @AbraCadaver That did it! You have just saved me a lot of time, effort and headaches. Many thanks. You're welcome. wildteen88's code removes duplicate names from each row but there can still be duplicates from different rows. I just assumed that there would be duplicates across rows. Quote Link to comment https://forums.phpfreaks.com/topic/209918-php-help-unique-unserialize/#findComment-1095693 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.