rgrne Posted January 16, 2007 Share Posted January 16, 2007 I am using a two dimensional array to sort a bunch of lists alphabetically in a for loop. I load the information into the array by assigning its values to the array elements, sort by one column in the array, then load the next list, etc. Unfortunately, if one list is shorter than the one before it, the values of the old list will remain in the un-rewritten parts of the array. So I need to erase all the values of the array before the next go-round. It would be best if I could set the array back to its "pristine" condition, with no elements at all, or maybe one element equal to "null", or whatever. I tried using unset($array), but that got rid of the array so effectively that the script could no longer read new values into it. Then I tried unset on the individual elements, but it didn't seem to have any effect. Finally, I just went through the array and loaded it up with "ZZZZZZZZZZZ" strings, which would sort to the end where I could ignore them. It seems like there must be some elegant, single line function that does waht I want, but I can't find it. Does it exist? Link to comment https://forums.phpfreaks.com/topic/34408-simple-way-to-erase-all-the-values-of-an-array/ Share on other sites More sharing options...
makeshift_theory Posted January 16, 2007 Share Posted January 16, 2007 well if all else fails:[code]$haystack = array("If I come up then skip me","And me","Me 2");foreach($array as $key => $value) if(!in_array($key, $haystack) unset($array[$key]);[/code]That will clear all keys from your array and if you want an exception list I added that in there two. Link to comment https://forums.phpfreaks.com/topic/34408-simple-way-to-erase-all-the-values-of-an-array/#findComment-161951 Share on other sites More sharing options...
paul2463 Posted January 16, 2007 Share Posted January 16, 2007 you could use what you have but do the following[code]<?phpunset($array);$array = array();?>[/code] Link to comment https://forums.phpfreaks.com/topic/34408-simple-way-to-erase-all-the-values-of-an-array/#findComment-161953 Share on other sites More sharing options...
makeshift_theory Posted January 16, 2007 Share Posted January 16, 2007 [quote author=paul2463 link=topic=122641.msg506025#msg506025 date=1168957247]you could use what you have but do the following[code]<?phpunset($array);$array = array();?>[/code][/quote]Yes, that is definately much more simple. So basically if you need an exception list use some of the code up top mixed with some of the down here =). Link to comment https://forums.phpfreaks.com/topic/34408-simple-way-to-erase-all-the-values-of-an-array/#findComment-161954 Share on other sites More sharing options...
Recommended Posts
Archived
This topic is now archived and is closed to further replies.