Ninjakreborn Posted February 6, 2008 Share Posted February 6, 2008 Let's assume I have an array as follows: array( [0] = 13 [1] = 15 [2] = 17 [3] = 35 [4] = 65 [5] = 77 [6] = 83 [7] = 95 ) Assume that is my current array above. Now let's say I wanted to take and remove 35 from that array. So we would be left with all of those key/value pairs except the one's I wanted to remove. I need to figure out how to do roughly the same thing except it would be dynamic. I am creating an array of years (which I have). Then I have a list of excluded years. I ned to be able to remove all variables that are present from the excluded years list out of the valid years. For example I might have Years to keep array( [0] = 2004 [1] = 2005 [2] = 2006 [3] = 2007 [4] = 2008 [5] = 2009 [6] = 2010 ) Years to remove array ( [0] = 2006 [1] = 2009 ) Now how would I make it so that all of the years to remove, are taken out of the years to keep array so I just have the years left that I need? Quote Link to comment Share on other sites More sharing options...
schilly Posted February 6, 2008 Share Posted February 6, 2008 nested while loops while(cycle yearstokeep){ while(cycle yearstoremove){ if(yearstokeep[i] == yearstoremote[j]) delete yearstokeep[i] } } Quote Link to comment Share on other sites More sharing options...
Ninjakreborn Posted February 6, 2008 Author Share Posted February 6, 2008 Ah ok, but I found a good solution too. <?php // years to keep creation $years_list = $years; // add one year $years_list[] = min($years) - 1; // remove one year $years_list[] = max($years) + 1; // order them sort($years_list); // here is what I used to take and remove everything that is in years list that is in years $years_list = array_diff($years_list, $years); // reorder them again sort($years_list); ?> Quote Link to comment Share on other sites More sharing options...
schilly Posted February 6, 2008 Share Posted February 6, 2008 ah way better. don't know all the array cmds. Quote Link to comment 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.