Jump to content

Removing array keys


ThePsion5

Recommended Posts

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

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

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

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 account

Sign in

Already have an account? Sign in here.

Sign In Now
×
×
  • Create New...