输出中的重复记录

I have been converting my scripts from mysql to mysqli and I am having trouble with the output of this script. This script pulls zip codes from a database based on user input and then finds the registered zip codes within a radius defined by user (in this case 25 miles). Then the script takes those zip codes and compares them to an active database of shops in the area and provides the closes shop based on the user zip code. All of that works fine. The problem is with my output (as you can see) it is returning duplicates and looping for each zip code found even though there is only one shop in the vicinity.

<?php
$servername = "localhost";
$username = "root";
$password = "password";
$dbname = "database";

// Create connection
$conn = new mysqli($servername, $username, $password, $dbname);
// Check connection
if ($conn->connect_error) {
    die("Connection failed: " . $conn->connect_error);
} 

// select statment
$zipcodes = 'SELECT zip_code, latitude, longitude, statename, city,
    ROUND((ACOS((SIN(33.776/57.2958) * SIN(latitude/57.2958)) +
    (COS(33.776/57.2958) * COS(latitude/57.2958) *
    COS(longitude/57.2958 - -95.7128/57.2958))))
    * 3963) AS distance
    from tblzipcode
    WHERE (latitude >= 33.776 - (25/111))
    And (latitude <= 33.776 + (25/111))
    AND (longitude >= -95.7128 - (25/111))
    AND (longitude <= -95.7125 + (25/111))
    ORDER BY distance';
$result = $conn->query($zipcodes);
if ($result->num_rows > 0) {
while($row = $result->fetch_assoc()) {
    $optioncodesOut = $row['zip_code'] . ",";
    echo $optioncodesOut ;

$nextplace ="select id, attn, addr1, addr2, addr3, city, state, country, zip, phone_number, fax, web_site_url, delaerwebid, ARCUSTO_ID, CUSTNO as dealer from DEALERLOCATOR where zip in (75425,75486,75421,75473,75460,75461,75470,75462,75462,75411,75477,74738,74759,75488,75446,74727,75416,74743,75450,75492,74723,74723,75443,75468,75438,75469,74721,74756,75434,75435,75441,75447,75432,75415,75436,75449,74726,75418,74760,75476,74523,74735,74735,75475,74720,75412,75448,74741,74741,74761,74542,74542,75413,75439)";
$result2 = $conn->query($nextplace);
if ($result2->num_rows > 0) {
while($row2 = $result2->fetch_assoc()) {
    $pickthedealer = $row2['zip'] . '&nbsp;' . $row2['attn'] . $row2['ARCUSTO_ID'];
    //echo $pickthedealer . '<br />';

$showplace = "select distinct zip, ARCUSTO_ID, attn from DEALERLOCATOR where ARCUSTO_ID in (25739)";
$result3 = $conn->query($showplace);
if ($result3->num_rows > 0) {
while($row3 = $result3->fetch_assoc()) {
    echo $row3['zip'] .'&nbsp;'. $row3['attn'] . '<br />';
      }
     }
    } 
   }
  }
 }
//echo $nextplace;

?>

I have made some changes to the code above and now the output looks like this:

75425,74743 Tall Tale Trailers Inc
75486,74743 Tall Tale Trailers Inc
75421,74743 Tall Tale Trailers Inc
75473,74743 Tall Tale Trailers Inc
75460,74743 Tall Tale Trailers Inc
75461,74743 Tall Tale Trailers Inc
75470,74743 Tall Tale Trailers Inc
75462,74743 Tall Tale Trailers Inc
75462,74743 Tall Tale Trailers Inc
75446,74743 Tall Tale Trailers Inc

As you can see there are 10 zip codes in the 25 mile vicinity however there is only one shop within those 10 zip codes. I need help getting it to display only one shop not same shop because it found 10 zip codes. It is not a unique issue as zip codes are already unique. The issue is that it is pulling the same dealer for each zip code when in reality there are 9 irrelevant listings.

I think you are including it in array. So for each iteration it is adding up in $categories array. You can use if condition of in_array or array_unique

while($row2 = $result2->fetch_array()) {
    $categories[$row2['zip']] = $row2;
}

It actually turned out to be a nesting issue.