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

Link to comment
Share on other sites

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

Link to comment
Share on other sites

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.

Link to comment
Share on other sites

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.

Link to comment
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • 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.