Training, Open Source computer languages
PerlPHPPythonMySQLApache / TomcatTclRubyJavaC and C++LinuxCSS 
Search for:
Home Accessibility Courses Diary The Mouth Forum Resources Site Map About Us Contact
Deleting duplicates from an array

Posted by Chris_Isaac (Chris Isaac), 18 June 2003
Hi

I'm trying to delete duplicate values from an array.  1st I'm pulling 2 varibles together (town and county fields), themn putting into an array, but the array_unique function is failing.  The output says it must be an array.  I've tried various options... help... please....

Thanks

Chris

Code:
<head><title>Simple search page</title><head>

<body>

<form method="post">
Please enter a town name :<input type="text" size=40 name="town">
<input type="submit" name="submit" value="submit">
<br><br>

<?php
$n=0;
$town=$_POST["town"];

$dbcon = @mysql_connect("localhost", "name", "pass") or die("Cannot connect to server");
if ($dbcon) { echo ("connected to server<br>");}

$db= @mysql_select_db("accom", $dbcon) or die("Cannot connext to DB");
if ($db) { echo ("connected to DB<br>");}

$sql="select accom,phone,town,county from test";

$query=mysql_query($sql,$dbcon);

while ($row=mysql_fetch_array($query)){

$combined= array($row["town"].", ".$row["county"]);
print (array_unique("$combined<br>"));
$n++;

}


?>
</body>



Posted by admin (Graham Ellis), 18 June 2003
Can I paraphrase the question to make sure I understand it? You have an arrayt containing a town and a county, and you want to eliminate one of them if they're both the same.   Thus
   Melksham    Wiltshire
will remain as a two element array but
   London  London
would be reduced to a single element.

I think this is the sort of thing you need to do:
Code:
$combined = array($row["town"],$row["county"]);
$really = array_unique($combined);
print (implode(", ",$really));


I'm not 100% sure I followed the question right ... so I haven't put in the time to test this / develop a working demo.  Let me know whether this is the solution you need or if I'm not on track.

Posted by Chris_Isaac (Chris Isaac), 18 June 2003
Hi

Not quite, I'm pulling the town/county fields out of a table which has multiple entryies for the same town county, eg:

chris 123 Bloggs street, Newport, Gwent
Nicky 234 You Street, Newport, IOW
fred 543 Cindy Street, Newport, Gwent

so when I pullout the Town/County I would get:

Newport, Gwent
Newport, IOW
Newport, Gwent


from this I only want 1 entry for Newport, Gwent. and 1 entry for Newport IOW.   Hope this makes sense.

Chris

Posted by admin (Graham Ellis), 18 June 2003
OK ... then within your while loop that fetched the data, you want to make up the $combined variable as it was in the code you originally posted.   But don't print it out ... instead, array_push it onto an array (let's call it $towns for the sake of argument.

Outside (after) the loop:

Code:
$towns = array_unique($towns);
print (implode("<BR>",$towns);


p.s. remember that before you push onto an array, it must already exist.   Create it with
      $towns = array();

Posted by Chris_Isaac (Chris Isaac), 19 June 2003
Hi, sorry, I'm still having trouble.  If I run the script below, $towns[0] in the while loop just prints out Array for each entry.  $towns[0] after the array_unique just has 1 entry.  I've tried putting that in a foreach loop but it doesn't change.

chris

Code:
<head><title>Simple search page</title><head>

<body>

<?php
$n=0;
$towns=array();
$combined=array();

$dbcon = @mysql_connect("localhost", "name", "pass") or die("Cannot connect to server");
if ($dbcon) { echo ("connected to server<br>");}

$db= @mysql_select_db("accom", $dbcon) or die("Cannot connext to DB");
if ($db) { echo ("connected to DB<br>");}

$sql="select accom,phone,town,county from test";

$query=mysql_query($sql,$dbcon);

while ($row=mysql_fetch_array($query)){

$combined= array($row["town"].", ".$row["county"]);
array_push($towns, $combined);
$n++;
echo $towns[0]."<br>";
}
$towns = array_unique($towns);
print (implode("<BR>",$towns[0]));

?>
</body>





Posted by admin (Graham Ellis), 19 June 2003
Oops - you don't want to make each $combined into an array; afraid my comment in the previous post was a bit misleading.  Make up the string of the town and county as just an ordinary variable and array_push it ... then it should work.  

Posted by Chris_Isaac (Chris Isaac), 19 June 2003
Perfect, thankyou.



This page is a thread posted to the opentalk forum at www.opentalk.org.uk and archived here for reference. To jump to the archive index please follow this link.

You can Add a comment or ranking to this page

© WELL HOUSE CONSULTANTS LTD., 2014: Well House Manor • 48 Spa Road • Melksham, Wiltshire • United Kingdom • SN12 7NY
PH: 01144 1225 708225 • FAX: 01144 1225 899360 • EMAIL: info@wellho.net • WEB: http://www.wellho.net • SKYPE: wellho