nicholasstephan Posted October 28, 2009 Share Posted October 28, 2009 I'm writing a class that implements Php's Iterator interface. Where I'm running into trouble is in the keys. I'd like to be able to respond differently to associative vs index keys, basically strings vs integers. Is there any way to check the input to the offsetGet() function. Using is_string and is_numeric don't work, because any string that happens to be numeric is treated as a numbered key, rather than an associative attribute name... So for example: echo $arr['hello']; works fine, 'hello' is a string, and the value matching that key is returned. echo $arr[23]; does alright, 23 is a number, and the 23rd value is returned. but what about: echo $arr["1231231234"]; I have no idea why you'd make a phone number an associative key, but nevertheless, I'd like it to return the correct response... the value associated with that key, and not look for the one billion two hundred thirty million...th entry. I'm a bit baffled. Cheers Quote Link to comment https://forums.phpfreaks.com/topic/179385-solved-type-checking-is_numeric-vs-is_string/ Share on other sites More sharing options...
nadeemshafi9 Posted October 28, 2009 Share Posted October 28, 2009 Consider the following array: $items = array( "banana" = > "fruit", "tomato" => "vegetable", "lentil" => "bean" ); $key = array_search('vegetable', $items); echo $key; may help Quote Link to comment https://forums.phpfreaks.com/topic/179385-solved-type-checking-is_numeric-vs-is_string/#findComment-946523 Share on other sites More sharing options...
mikesta707 Posted October 28, 2009 Share Posted October 28, 2009 try is_int() Quote Link to comment https://forums.phpfreaks.com/topic/179385-solved-type-checking-is_numeric-vs-is_string/#findComment-946527 Share on other sites More sharing options...
nicholasstephan Posted October 30, 2009 Author Share Posted October 30, 2009 is_int seems to do it! Why is_int seems to strictly type the input, while is_numeric doesn't, I don't know. By it works. thanks Quote Link to comment https://forums.phpfreaks.com/topic/179385-solved-type-checking-is_numeric-vs-is_string/#findComment-947944 Share on other sites More sharing options...
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.