mkosmosports Posted January 10, 2007 Share Posted January 10, 2007 Hey,IS there a way to run a loop on an array and echo only unique entries. Below is my array which contains several duplicates.$transferyear[] = substr($row["DATE_FORMAT(START_DATE,'%Y%m')"],0,4);Now Id like to run a loop and echo the unique values in that array.Help appreciated.Thanks! Quote Link to comment https://forums.phpfreaks.com/topic/33587-looping-through-an-array-an-not-extracting-any-duplicates/ Share on other sites More sharing options...
trq Posted January 10, 2007 Share Posted January 10, 2007 [code]<?php $duplicate_array = array("foo","bar","boo","foo","bar"); $clean_array = array_unique($duplicate_array); foreach($clean_array as $arr) { echo $arr."\n"; }?>[/code] Quote Link to comment https://forums.phpfreaks.com/topic/33587-looping-through-an-array-an-not-extracting-any-duplicates/#findComment-157321 Share on other sites More sharing options...
mkosmosports Posted January 10, 2007 Author Share Posted January 10, 2007 Damn, I was so close thorpe. Thats almost what I had but not quite. Thanks for your help once again! Quote Link to comment https://forums.phpfreaks.com/topic/33587-looping-through-an-array-an-not-extracting-any-duplicates/#findComment-157324 Share on other sites More sharing options...
obsidian Posted January 10, 2007 Share Posted January 10, 2007 Out of curiosity, is there a reason you can't query for DISTINCT from the database and avoid the hassle of having to parse it in your PHP script? Quote Link to comment https://forums.phpfreaks.com/topic/33587-looping-through-an-array-an-not-extracting-any-duplicates/#findComment-157333 Share on other sites More sharing options...
mkosmosports Posted January 10, 2007 Author Share Posted January 10, 2007 Well, initially thats what Im doing:$alltransferdates = @mysql_query("SELECT DISTINCT DATE_FORMAT(START_DATE,'%Y%m') FROM sf_team_player");Then:while($row = mysql_fetch_assoc($alltransferdates)){$transferdate[] = $row["DATE_FORMAT(START_DATE,'%Y%m')"];$transferyear[] = substr($row["DATE_FORMAT(START_DATE,'%Y%m')"],0,4);}I need to have the full date in an array hence $transferdate[].Then Im dropping the %Y values into a separate array $transferyear[] because I also need to extract all the unique year values later on, and I couldnt figure out another way to do it. Quote Link to comment https://forums.phpfreaks.com/topic/33587-looping-through-an-array-an-not-extracting-any-duplicates/#findComment-157341 Share on other sites More sharing options...
obsidian Posted January 10, 2007 Share Posted January 10, 2007 Well, I think thorpe may have the best idea, then, but let me suggest this to you as an alternative: try selecting the month and year separately and combining them into your first array, then inserting only the year into the second, if it's not already there:[code]<?php$q = "SELECT DISTINCT DATE_FORMAT(START_DATE,'%Y%m') AS date, YEAR(START_DATE) AS year FROM sf_team_player";$sql = mysql_query($q);for ($i = 0; $i < mysql_num_rows($sql); $i++) { $date = mysql_result($sql, $i, 'date'); $year = mysql_result($sql, $i, 'year'); $transferdate[] = $date; if (!in_array($year, $transferyear)) $transferyear[] = $year;}?>[/code]You end up with the same results, but it's just a different approach. Hope this helps! Quote Link to comment https://forums.phpfreaks.com/topic/33587-looping-through-an-array-an-not-extracting-any-duplicates/#findComment-157346 Share on other sites More sharing options...
mkosmosports Posted January 10, 2007 Author Share Posted January 10, 2007 Thanks Obsidian. I appreciate the time youve taken to suggest this method to me.Ive tried the code you suggested originally and I get a php error on this line though:if (!in_array($year, $transferyear)) $transferyear[] = $year; php is saying $transferyear is an undefined variable, I assume because its used before the array is defined? (I have global variables turned off, wonder if that could be why)Then I thought you made a type, since shouldnt it check if $year exists in the $transferdate array: if (!in_array($year, $transferdate)) $transferyear[] = $year;But that cant be it, because the $transfer year array no longer contains unique year values, it contains all of them.I guess its the global variables then. You think? Quote Link to comment https://forums.phpfreaks.com/topic/33587-looping-through-an-array-an-not-extracting-any-duplicates/#findComment-157392 Share on other sites More sharing options...
obsidian Posted January 10, 2007 Share Posted January 10, 2007 No, I think that all you need to do is declare your empty variables outside of the loop:[code]<?php$q = "SELECT DISTINCT DATE_FORMAT(START_DATE,'%Y%m') AS date, YEAR(START_DATE) AS year FROM sf_team_player";$sql = mysql_query($q);$transferdate = array();$transferyear = array();for ($i = 0; $i < mysql_num_rows($sql); $i++) { $date = mysql_result($sql, $i, 'date'); $year = mysql_result($sql, $i, 'year'); $transferdate[] = $date; if (!in_array($year, $transferyear)) $transferyear[] = $year;}?>[/code]Also, what the code is doing is [b]only[/b] adding the $year to the $transferyear array if it does not already exist in there. That's how it makes it a unique listing. Quote Link to comment https://forums.phpfreaks.com/topic/33587-looping-through-an-array-an-not-extracting-any-duplicates/#findComment-157409 Share on other sites More sharing options...
mkosmosports Posted January 10, 2007 Author Share Posted January 10, 2007 Youre the man obsidian. And dont worry, soon I wont be asking about what I asked about in my last post...lol. Soon Ill be figuring those out. I should of figured it out this time around.Your method is better for me because there is one more situation Id like to cover with it, and I have a gut feeling it will be easier. The reason I need to know all of the unique years is because there is a page where Id like to give the user links to all available data in the years that I have covered in by db. Now, there is one more thing though. Those urls containing the unique year dates also contain month parameters. I want to find a way I can get the max month from each unique year value. So,$q = "SELECT DISTINCT DATE_FORMAT(START_DATE,'%Y%m') AS date, YEAR(START_DATE) AS year FROM sf_team_player WHERE OLDCLUB_ID IS NOT NULL ORDER BY START_DATE DESC"; $alltransferdates = mysql_query($q); $transferyear = array(); for ($i = 0; $i < mysql_num_rows($alltransferdates); $i++) { $date = mysql_result($alltransferdates, $i, 'date'); $year = mysql_result($alltransferdates, $i, 'year'); $transferdate[] = $date; if (!in_array($year, $transferyear)) $transferyear[] = $year; }Then on the page where I want the dynamic links I have a loop which returns the number of unique years in the form of links. foreach ($transferyear as $tryear) { echo("<a href=\"players.html?view=transfers&season=$tryear&in=$in\">$tryear</a>"); }Now though, Id like to replace the $in parameter with the max month for that unique year.Now, Ive tried some things, but Ive had no success. Im thinking I need to make $transferyear a multidimensional array containing also the max month for each year, but I cant get it together. Any ideas?Thanks! Quote Link to comment https://forums.phpfreaks.com/topic/33587-looping-through-an-array-an-not-extracting-any-duplicates/#findComment-157458 Share on other sites More sharing options...
mkosmosports Posted January 10, 2007 Author Share Posted January 10, 2007 Bumping it up in case obsidian pops up again. Obsidian, youre not gonna leave me hanging at the last hurdle.... :D Quote Link to comment https://forums.phpfreaks.com/topic/33587-looping-through-an-array-an-not-extracting-any-duplicates/#findComment-157703 Share on other sites More sharing options...
obsidian Posted January 11, 2007 Share Posted January 11, 2007 OK, I'll try not to leave you haning ;) ... Since you're already ordering by date DESC, you know that the first record you come across (the one from which you are populating your [b]year[/b] array) is going to have the latest date for that year. With that in mind, just modify your query to return the month as well, and you can have both together:[code]<?php$q = "SELECT DISTINCT DATE_FORMAT(START_DATE,'%Y%m') AS date, YEAR(START_DATE) AS year, MONTH(START_DATE) AS month FROM sf_team_player WHERE OLDCLUB_ID IS NOT NULL ORDER BY START_DATE DESC";$alltransferdates = mysql_query($q);$transferyear = array();$maxmonth = array();for ($i = 0; $i < mysql_num_rows($alltransferdates); $i++) { $date = mysql_result($alltransferdates, $i, 'date'); $year = mysql_result($alltransferdates, $i, 'year'); $month = mysql_result($alltransferdates, $i, 'month'); $transferdate[] = $date; if (!in_array($year, $transferyear)) { $transferyear[] = $year; $maxmonth[] = $month }}?>[/code]Then, once you have the two arrays, you can loop through them together and associate the year to the max month:[code]<?phpforeach ($transferyear as $key => $val) { $month = $maxmonth[$key]; echo "$month is the max month for the year $val<br />\n";}?>[/code]Does this help some? Quote Link to comment https://forums.phpfreaks.com/topic/33587-looping-through-an-array-an-not-extracting-any-duplicates/#findComment-158212 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.