Jump to content

[SOLVED] type checking (is_numeric vs. is_string)


nicholasstephan

Recommended Posts

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

Link to comment
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • 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.