Jump to content

Simple way to erase all the values of an array?


rgrne

Recommended Posts

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?
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.
[quote author=paul2463 link=topic=122641.msg506025#msg506025 date=1168957247]
you could use what you have but do the following
[code]
<?php

unset($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 =).

Archived

This topic is now archived and is closed to further replies.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.