tec-4 Posted September 7, 2011 Share Posted September 7, 2011 Hi All, I essentially am trying to figure out how to print out all of the zip codes that are associated with a search that I am doing, but not list the same zip code more than once. My current foreach statement is as follows and prints out the zip code for each property that is associated with the search: <ul> <?php foreach($properties as $property) { ?> <li><?=$property->$zipcode?></li> <?php } ?> </ul> How would I modify this to print out 1 occurrence of each zip code that is in the search, even if, for example 20 properties have the same zip code? Thanks in advance! P.S. - Tried using an array to work with it but pretty sure my logic was incorrect when putting it together and couldn't get past the errors. Anyways, thanks in advance for the help! Link to comment https://forums.phpfreaks.com/topic/246667-foreach-help/ Share on other sites More sharing options...
xyph Posted September 7, 2011 Share Posted September 7, 2011 Can you print_r() your $properties variable? Perhaps post your class that $property holds? Here's a quick and dirty solution: <?php $duplicates = array('world','world','ipsum','etc','world','ipsum','baz','etc','baz', 'hello','baz','world','baz','hello','foo','etc','ipsum','ipsum','baz','ipsum','world', 'hello','hello','ipsum','baz'); $echoed = array(); foreach( $duplicates as $value ) { if( in_array($value,$echoed) ) continue; echo $value.' '; $echoed[] = $value; } ?> Link to comment https://forums.phpfreaks.com/topic/246667-foreach-help/#findComment-1266611 Share on other sites More sharing options...
msaz87 Posted September 7, 2011 Share Posted September 7, 2011 array_unique will do it Link to comment https://forums.phpfreaks.com/topic/246667-foreach-help/#findComment-1266614 Share on other sites More sharing options...
xyph Posted September 7, 2011 Share Posted September 7, 2011 Not if $properties holds an array of objects. Link to comment https://forums.phpfreaks.com/topic/246667-foreach-help/#findComment-1266634 Share on other sites More sharing options...
btherl Posted September 8, 2011 Share Posted September 8, 2011 tec-4, Is that $property->$zipcode or $property->zipcode? Link to comment https://forums.phpfreaks.com/topic/246667-foreach-help/#findComment-1266650 Share on other sites More sharing options...
tec-4 Posted September 13, 2011 Author Share Posted September 13, 2011 sorry, its taken me so long to get back to this - been out of town, anyways thank you all for your responses. @ btherl, sorry, that was a type. Supposed to be "$property->zipcode" @xyph - don't think i can use print_r() on it since all the properties are in a database (could be wrong tho, fairly new to the game). But essentially I have thousands of "properties" in a database where i'd like to find unique instances of values within those properties (columns in the DB), such as the zip codes. @msaz87 - I'll have to look into the array_unique() function, havent used that before - I'll definitely be looking into it, thanks for the suggestion. Link to comment https://forums.phpfreaks.com/topic/246667-foreach-help/#findComment-1269014 Share on other sites More sharing options...
Pandemikk Posted September 13, 2011 Share Posted September 13, 2011 You should be pulling unique zip codes from the database, shouldn't you? If the you're using MySQL: SELECT DISTINCT zip code; should do the trick. Link to comment https://forums.phpfreaks.com/topic/246667-foreach-help/#findComment-1269015 Share on other sites More sharing options...
Recommended Posts
Archived
This topic is now archived and is closed to further replies.