0

How could I build a unique array check each of the address fields.

E.g at the moment I get everything:

$stmnt = "SELECT `location_id`, `location_address1`, `location_address2`,
            `location_town`, `location_region`, `location_postcode`
          FROM locations WHERE user_id = '{$id}'";
$results = $db->fetchAll($stmnt);

    if(!empty($results ))  {
        foreach($results as $row) {
            if($unique){
                $value = $row['location_id'];
                $label = implode(", ", array(
                    'address1'      => $row['location_address1'],
                    'address2'      => $row['location_address2'],
                    'town'          => $row['location_town'],
                    'region'        => $row['location_region'],
                    'postcode'      => $row['location_postcode']
                ));
            }

I was thinking that where if($unique){ is you would check this address1, address2, etc exist in the temp array by searching this some how?

John Magnolia
  • 15,835
  • 30
  • 147
  • 254
  • 2
    Why not just do a `SELECT DISTINCT ...`? – Sean Bright Sep 27 '12 at 19:38
  • do u want unique rows, or unique values inside each row? – Teena Thomas Sep 27 '12 at 19:42
  • That may work, but that returns the distinct {location_id, location_address1, ... location_postcode} pairs. I'm not 100% sure if that's what OP wants or if they want all the distinct location_id, all the distinct location_address1, etc. – Mr. Llama Sep 27 '12 at 19:42
  • 1
    you could use `!in_array($uniqueColumn, $tmpArray)` and push your unique column in `$tmpArray` ( http://php.net/manual/en/function.in-array.php ) . – pce Sep 27 '12 at 19:43
  • When I add a DISTINCT this doesn't make any difference and always returns the same amount of rows. I am guessing because of the primary key `location_id` – John Magnolia Sep 27 '12 at 19:55
  • @pce how would I do the in_array for a multi-dimensional array. As in search all of the indexes with the same name? – John Magnolia Sep 27 '12 at 20:15
  • If you really need it, there are functions in the comments of the manual of `in_array` or here: http://stackoverflow.com/questions/4128323/in-array-and-multidimensional-array. But, why multidimensional? You have one row, which column is unique? If it is not one column only, then you could concat columns and safe the hash of it, like `$uniqueCol = sha1($row['location_address1'].";".$row['location_postcode']);`, push the hash in `$tmpArray` and check if you already know the address with in_array. – pce Sep 27 '12 at 20:52

1 Answers1

2

Using SELECT DISTINCT will allow you to bring back a unique set of results, excluding results that are dupliciates of another. You can even still use the ORDER BY if you intend to, but remember that with a DISTINCT selection, anything in the ORDER BY must also be in the SELECT statement.

Flosculus
  • 6,660
  • 3
  • 15
  • 39
  • Is this affected by the primary key and other fields I have in the row? I didn't show them because I didm't think it would be relevant – John Magnolia Sep 27 '12 at 20:01
  • yes, having a unique column in the select statement would defeat the point of the DISTINCT keyword. Some database types might behave differently however, I would ommit the ID if you dont need it. – Flosculus Sep 27 '12 at 20:11