ThePsion5 Posted July 7, 2006 Share Posted July 7, 2006 Hi guys,A function I'm creating iterates through an array with a certain number of important values by seeing if the appropriate array key exists. I also want to store any other values in the array, but be able to use the count() function to get an accurate number of values, not including the important values mentioned above. This was my first attempt: //Retrieve the important values$this->important = $Attributes['important'];$this->important2 = $Attributes['important2'];$this->important3 = $Attributes['important3'];//Remove them from the array$Attributes['important'] = null;$Attributes['important2'] = null;$Attributes['important3'] = null;//Save the rest as unimportant values$this->Misc = $Attributes; This removes the information from the array, but when I call count on it, it still counts the empty array keys (so if there were 3 important attributes and 3 unimportant ones, count() still returns 6 after i've set the value of the important keys to null).I'd rather not have to make a coding structure to copy the unimportant array values, mostly because i'd be a pain in the arse. Is there a way for me to remove keys from an array? Link to comment Share on other sites More sharing options...
aspnetguy Posted July 7, 2006 Share Posted July 7, 2006 you could store the number of important values as a constant just before you null them.Then when you call count you can subtract the constant you saved earlier. Link to comment Share on other sites More sharing options...
ThePsion5 Posted July 7, 2006 Author Share Posted July 7, 2006 Yeah, I was just hoping I wouldn't have to do that, lol, since I also want to be able to access and iterate through the $Misc array without having to worry about empty keys. It doesn't look like there's any other way to do it, unfortunately. Link to comment Share on other sites More sharing options...
zanfranceschi Posted July 7, 2006 Share Posted July 7, 2006 after unset($array['key']), this isn't counted anymore. Link to comment Share on other sites More sharing options...
ThePsion5 Posted July 7, 2006 Author Share Posted July 7, 2006 Thanks, that's just what I was looking for! I didn't think I could use unset() with array keys, that's why I hadn't tried it. Link to comment Share on other sites More sharing options...
aspnetguy Posted July 7, 2006 Share Posted July 7, 2006 I figured there must be something to do that, there usually is in PHP, I just don't know the functions very well. Link to comment Share on other sites More sharing options...
justsomeguy Posted July 7, 2006 Share Posted July 7, 2006 It sounds like a read through the array functions reference would be fun for you. Check out this one:http://www.php.net/manual/en/function.array-diff.phpDid I mention that I always try writing code where I later determine that there is a built-in function to do the same thing?$new_list = array_diff($Attributes, $values_to_remove);There is also array_diff_key (what you need), array_diff_assoc, functions to define your own callback functions, whatever a growing boy needs! Link to comment Share on other sites More sharing options...
Recommended Posts
Create an account or sign in to comment
You need to be a member in order to leave a comment
Create an account
Sign up for a new account in our community. It's easy!
Register a new accountSign in
Already have an account? Sign in here.
Sign In Now