ShadowMage Posted March 5, 2010 Report Share Posted March 5, 2010 Hi all, I have a situation. Say I have the following array:$arrInArray = array(array("first" => 1, "second" => 2, "third" => 3), array("zero" => "none", "one" => "single", "two" => "double"))How can I find out if a value exists in the array without looping through the first dimension manually. If I do this... if (array_key_exists("first", $arrInArray)) { echo "\"first\" is in array<br />\n";}if (in_array("single", $arrInArray)) { echo "\"single\" is in array<br />\n";} ...nothing is echoed. Both if statements evaluate false. If I do this... if (array_key_exists("first", $arrInArray[0])) { echo "\"first\" is in array<br />\n";}if (in_array("single", $arrInArray[1])) { echo "\"single\" is in array<br />\n";} ...both statements are echoed.Is it possible to get this to work without creating a custom function with a manual loop to run through the first dimension of the array? My concern is that a custom function will chew on more resources than a builtin function. Is that true? Since the array that I'm going to be looping through will be somewhat large, this is a relatively big concern. Link to comment Share on other sites More sharing options...
justsomeguy Posted March 5, 2010 Report Share Posted March 5, 2010 You could use array_walk_recursive to do that, the downside is even if you find the element you're looking for first, it will still call the function for each element. http://www.php.net/manual/en/function.arra...k-recursive.phpA custom function could stop searching as soon as it finds a match. Link to comment Share on other sites More sharing options...
ShadowMage Posted March 5, 2010 Author Report Share Posted March 5, 2010 OK, thanks JSG. That answers my question! 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