Jump to content

php help? (unique unserialize)


woop

Recommended Posts

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.

Link to comment
https://forums.phpfreaks.com/topic/209918-php-help-unique-unserialize/
Share on other sites

$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 />'; 
    } 
}

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 />'; 
} 

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.

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.

Archived

This topic is now archived and is closed to further replies.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.