Jump to content

Check difference between numeric string and number


Zephni

Recommended Posts

This sounds simple in the title, but let me explain...

 

If I did the below:

$array = array("1" => "value");

foreach($array as $key => $value)
	if(is_string($key))
		echo "true";
	else
		echo "false";

The result would be false, even though the "1" passed as a key is a string prior to the foreach loop.

 

Is there a way to check the ACTUAL type of the $key in this situation without just determining whether it "can" be a int or "can" be a string. If I did:

$array = array("1" => "value");

foreach($array as $key => $value)
	echo gettype($key);

The result would be "integer" so It looks to be that if the foreach loop determines if the string "can" be an integer then it regards it as such. Is there anyway around this?

 

Thanks in advance for any help :)

 

(Please note I tried to change the title of this post because I realized it wasn't quite specific enough regarding arrays but it won't let me change the title)

Edited by Zephni
Link to comment
Share on other sites

This has nothing to do with is_string() or gettype(). It's simply how arrays work: When you use a string key which happens to be a valid decimal representation, the key is automatically converted to an integer.

 

Why is this a problem? In other words, why do you insist on distinguishing between integers and decimal strings? PHP is a loosely typed language, so this shouldn't matter at all.

  • Like 1
Link to comment
Share on other sites

The reason is, is because I need to tell whether the array was defined with keys and values by the programmer, rather than a single dimensional array. It will be hard to explain exactly why, but it is necessary because what I'm building needs to be able to tell the difference between an array that looks like this:

array("item1", "item2");

And this:

array("0" => "item1", "1" => "item2");

Because if the key is defined by the user (programmer) the foreach loop needs to perform an extra task. The more I look into this. I should probably do what I'm doing a completely different way.. but I guess the answer is clear... PHP does whatever it wants with types and we have no control over it :P

Link to comment
Share on other sites

Like I said, PHP is a loosely typed language. Implicit type conversion is really the core feature of this language.

 

So, yes, you'll definitely need to take a different approach. Instead of relying on type detection magic, make an explicit distinction between your two cases (maybe a flag will help, maybe you should use two separate functions).

Link to comment
Share on other sites

I agree with Jacques that there is probably a better way, however given the limited info provide, it seems that you just need to know whether an array is flat or multi-dimensional.

 

 

The reason is, is because I need to tell whether the array was defined with keys and values by the programmer, rather than a single dimensional array. It will be hard to explain exactly why, but it is necessary because what I'm building needs to be able to tell the difference between an array that looks like this:

array("item1", "item2");

And this:

array("0" => "item1", "1" => "item2");

Because if the key is defined by the user (programmer) the foreach loop needs to perform an extra task. The more I look into this. I should probably do what I'm doing a completely different way.. but I guess the answer is clear... PHP does whatever it wants with types and we have no control over it :P

 

 

 

The problem is that for php, there is no difference between the 2 arrays you show.   In your example, you are defining them differently, but the end result is the same:

 

 

$a = array("item1", "item2");
$b = array("0" => "item1", "1" => "item2");
 
var_dump($a);
var_dump($b);

 

You'll get this result:

 

 

array(2) {
  [0]=>
  string(5) "item1"
  [1]=>
  string(5) "item2"
}
array(2) {
  [0]=>
  string(5) "item1"
  [1]=>
  string(5) "item2"
}

 

Again I'm not sure if this is helpful to you, but when you don't specifically indicate the array key for a flat array, it will be zero based.  When you do specify an array key of any type, that will be honored by php.  So if you started your other arrays at "1" you could then tell the difference between each type on the basis of whether or not a zero-th element exists or not.

 

See this:

 

 

$a = array("item1", "item2");
$b = array("1" => "item1", "2" => "item2");
 
var_dump($a);
var_dump($b);
 
function hasZeroElement($array) {
  return isset($array[0]);
}
 
 
if (hasZeroElement($a)) {
  echo "A has zero index";
} else {
  echo "A has NO zero index";
}
echo "\n";
 
if (hasZeroElement($b)) {
  echo "B has a zero index";
} else {
  echo "B has NO zero index";
}
echo "\n";

 

Result!:

 

 

array(2) {
  [0]=>
  string(5) "item1"
  [1]=>
  string(5) "item2"
}
array(2) {
  [1]=>
  string(5) "item1"
  [2]=>
  string(5) "item2"
}
A has zero index
B has NO zero index
Link to comment
Share on other sites

is_numeric(...

 

 

Well that's actually the issue that Zephni stumbled upon.  Whether or not you specify it as a string, PHP will convert it to it's numeric equivalent.  This was done to avoid the possible confusion of people trying this:

$a = array(0 => 'numeric_zero', "0" => 'string_zero');

What happens in this case, is that whatever is declared last ends up in the array:

array(1) {
  [0]=>
  string(11) "string_zero"
}

PHP is trying to save people from themselves, I suppose.

Link to comment
Share on other sites

Well that's actually the issue that Zephni stumbled upon.  Whether or not you specify it as a string, PHP will convert it to it's numeric equivalent.

 

I realized that about a second after I hit post. Was still thinking $array['key1'] = "blah" vs. $array['1'] = "blah" instead of $array['1'] = "blah" vs $array[1] = "blah".

 

I was kinda hoping no-one had noticed before I got the chance to remove the evidence... :)

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.