TheBrandon Posted April 25, 2012 Share Posted April 25, 2012 Right now I have an array of zipcodes with open invitations; I want to loop over the array of zipcodes, query the database and get the name of each zipcode and then remove duplicate names. So if 90210, 90211 and 90212 are in the array with the first 2 being City A and the last one being City B, I just want City A and City B, not City A, City A, City B. Right now I'm doing this: foreach($invitation_Zipcodes as $key => $value){ echo 'Key: '.$key.' Value: '.$value.'<br/>'; $csaname_sql = "SELECT CBSA_Name FROM ZIPCodes WHERE ZipCode = '$value'"; $csaname_result = mysql_query($csaname_sql); $csaname_array[] = mysql_fetch_assoc($csaname_result); } //$csaname_array = array_unique($csaname_array); print_r($csaname_array); echo '<hr/>'; print_r($invitation_Zipcodes); exit; That outputs this: Array ( [0] => Array ( [CBSA_Name] => Crestview-Fort Walton Beach-Destin, FL ) [1] => Array ( [CBSA_Name] => Crestview-Fort Walton Beach-Destin, FL ) [2] => Array ( [CBSA_Name] => Crestview-Fort Walton Beach-Destin, FL ) [3] => Array ( [CBSA_Name] => Crestview-Fort Walton Beach-Destin, FL ) [4] => Array ( [CBSA_Name] => Pensacola-Ferry Pass-Brent, FL ) ) <hr/>Array ( [0] => 32548 [1] => 32579 [2] => 32578 [3] => 32580 [7] => 32501 ) If I uncomment the unique call, it outputs this: Array ( [0] => Array ( [CBSA_Name] => Crestview-Fort Walton Beach-Destin, FL ) ) <hr/>Array ( [0] => 32548 [1] => 32579 [2] => 32578 [3] => 32580 [7] => 32501 ) What am I doing wrong? Why is it losing "Pensacola-Ferry Pass-Brent, FL" ? Quote Link to comment https://forums.phpfreaks.com/topic/261589-help-with-making-array-unique/ Share on other sites More sharing options...
PFMaBiSmAd Posted April 25, 2012 Share Posted April 25, 2012 You can do this all directly in your query - $csaname_sql = "SELECT DISTINCT CBSA_Name FROM ZIPCodes WHERE ZipCode IN ('".implode("','",$invitation_Zipcodes)."')"; There's almost never a time when you should put a SELECT query inside of a loop, due to the performance hit you take. There is always a way to get one query to retrieve all the rows you want at one time. Quote Link to comment https://forums.phpfreaks.com/topic/261589-help-with-making-array-unique/#findComment-1340469 Share on other sites More sharing options...
xyph Posted April 25, 2012 Share Posted April 25, 2012 Use the IN clause to do this all in a single query. Use the DISTINCT clause to select unique values only. Here's an example. Don't copy this code into a production environment, use it for educational purposes <?php $zips = array( '90210','90211','90212' ); $inString = '"' . implode('","', $zips) . '"'; $query = 'SELECT DISTINCT CBSA_Name FROM ZIPCodes WHERE ZipCode IN ('.$inString.')'; echo 'The query will be: '.$query.'<br>'; $result = mysql_query($query); if( !$result ) die('Error: '.mysql_error()); echo '<h3>Results</h3>'; while($row = mysql_fetch_row($result)) echo $row[0].'<br>'; ?> edit - What he said. Quote Link to comment https://forums.phpfreaks.com/topic/261589-help-with-making-array-unique/#findComment-1340470 Share on other sites More sharing options...
TheBrandon Posted April 25, 2012 Author Share Posted April 25, 2012 I'll definitely try the solutions you have recommended. Thanks both of you. In an effort to further understand how the array_unique command works, could you still tell me why that wasn't working the way I wanted? I fully intend to switch to your SQL version of the code, I'd just like to learn why it didn't work for future projects. Quote Link to comment https://forums.phpfreaks.com/topic/261589-help-with-making-array-unique/#findComment-1340472 Share on other sites More sharing options...
PFMaBiSmAd Posted April 25, 2012 Share Posted April 25, 2012 Array_unique is not working because you are storing the array that mysql_fetch_assoc returns into your main array, not just the CBSA_Name value out of the array. Quote Link to comment https://forums.phpfreaks.com/topic/261589-help-with-making-array-unique/#findComment-1340476 Share on other sites More sharing options...
xyph Posted April 25, 2012 Share Posted April 25, 2012 To elaborate, array_unique is only meant for a single-dimensional array. You've tried to use a two-dimensional array. <?php $array1 = array( 'Apple','Banana','Apple','Orange' ); $array2 = array( array('Apple'), array('Banana'), array('Apple'), array('Orange') ); header('Content-Type: text/plain'); // Display page as text, not html echo 'Array 1: '; print_r($array1); echo 'Array 2: '; print_r($array2); echo 'Array 1 Unique: '; print_r(array_unique($array1)); echo 'Array 2 Unique: '; print_r(array_unique($array2)); ?> Because every array within $array2 resolves to the string 'Array', it sees them all as identical. Quote Link to comment https://forums.phpfreaks.com/topic/261589-help-with-making-array-unique/#findComment-1340477 Share on other sites More sharing options...
TheBrandon Posted April 25, 2012 Author Share Posted April 25, 2012 Array_unique is not working because you are storing the array that mysql_fetch_assoc returns into your main array, not just the CBSA_Name value out of the array. Could you elaborate on how to just pull the CBSA_Name value? I'm still learning a lot when it comes to manipulating arrays so I think this is something I do frequently and would like to know how to do properly. Quote Link to comment https://forums.phpfreaks.com/topic/261589-help-with-making-array-unique/#findComment-1340479 Share on other sites More sharing options...
TheBrandon Posted April 25, 2012 Author Share Posted April 25, 2012 To elaborate, array_unique is only meant for a single-dimensional array. You've tried to use a two-dimensional array. <?php $array1 = array( 'Apple','Banana','Apple','Orange' ); $array2 = array( array('Apple'), array('Banana'), array('Apple'), array('Orange') ); header('Content-Type: text/plain'); // Display page as text, not html echo 'Array 1: '; print_r($array1); echo 'Array 2: '; print_r($array2); echo 'Array 1 Unique: '; print_r(array_unique($array1)); echo 'Array 2 Unique: '; print_r(array_unique($array2)); ?> Because every array within $array2 resolves to the string 'Array', it sees them all as identical. Thank you so much for your in-depth reply. I definitely understand what you're saying. I didn't realize array_unique was solely meant for single-dimension arrays. Thanks again! Quote Link to comment https://forums.phpfreaks.com/topic/261589-help-with-making-array-unique/#findComment-1340480 Share on other sites More sharing options...
xyph Posted April 25, 2012 Share Posted April 25, 2012 To elaborate on PFMaBiSmAd's comment, you want to use this code instead: <?php foreach($invitation_Zipcodes as $key => $value){ echo 'Key: '.$key.' Value: '.$value.'<br/>'; $csaname_sql = "SELECT CBSA_Name FROM ZIPCodes WHERE ZipCode = '$value'"; $csaname_result = mysql_query($csaname_sql); $csaname_data = mysql_fetch_row($csaname_result); $csaname_array[] = $csaname_data[0]; } print_r($csaname_array); ?> Quote Link to comment https://forums.phpfreaks.com/topic/261589-help-with-making-array-unique/#findComment-1340481 Share on other sites More sharing options...
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.