当数组的父值是字符串而不是数组时,isset 在数组上返回 true
我的一个阵列遇到了一个奇怪的问题。数组看起来像这样,然后我对它执行 print_r() :
Array
(
[id] => 1688
[pCid] => 1992
[category] => 177
[archive] => 0
[catid] =>
[pid] =>
[order_nr] => 1
[cOrder] => 1
[cSeo] => no-colour
[order_id] => 0
[price] => 8.99
[sale_price] => 7.99
[sale_expiry] =>
[oPrices] => 7.99
[member_price] => 6.99
[set] => 0
)
当我做一个简单的操作时
if(isset($array['set']['stock'])){}
,当 $array['set'] 当前等于 0 时,它返回 true。
var_dump(isset($array['set']['stock'])) //bool(true)
返回 true
任何想法为什么会发生这种情况?
(我通过执行 is_array($array['set']) 检查解决了检查问题,但仍然不确定为什么 isset 不起作用)
我注意到您可以像这样复制相同的问题:
<?php
$colour = array();
$colour['set'] = '0';
var_dump(isset($colour['set']['stock'])); //will return true
?>
I encountered a strange issue with one of my arrays. The array looks like this then I do a print_r() on it:
Array
(
[id] => 1688
[pCid] => 1992
[category] => 177
[archive] => 0
[catid] =>
[pid] =>
[order_nr] => 1
[cOrder] => 1
[cSeo] => no-colour
[order_id] => 0
[price] => 8.99
[sale_price] => 7.99
[sale_expiry] =>
[oPrices] => 7.99
[member_price] => 6.99
[set] => 0
)
and when I do a simmple
if(isset($array['set']['stock'])){}
it returns true when the $array['set'] currentlly equals 0.
var_dump(isset($array['set']['stock'])) //bool(true)
returns true
Any ideas why is this happening?
(I resolved the check issue by doing is_array($array['set']) check but still not sure why is the isset not working)
I noticed that you can replicate the same issue like this:
<?php
$colour = array();
$colour['set'] = '0';
var_dump(isset($colour['set']['stock'])); //will return true
?>
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
字符串是字符数组。当比较 ['stock'] 数组时,它会比较第一个字符,因为它正在查看字符串。
例如返回“f”。
An string is an array of characters. When the ['stock'] array is compared it compares the first character as it's looking at a string.
Returns 'f' for example.
在 PHP 中(与大多数其他语言一样),字符串可以作为字符数组进行访问。
因此,当您执行
$str['stock']
时,您实际上正在访问$str
中的'stock'
偏移量。由于字符串只有数字偏移量,因此字符串'stock'
被转换为整数,并且由于它不包含任何数字,因此结果为0
。因此,您可以访问$str[0]
,它是已设置的字符串的第一个字符。In PHP (as in most other languages) strings can be accessed as arrays of characters.
So when you do
$str['stock']
you really are accessing the'stock'
offset in the$str
. As strings have only numeric offsets the string'stock'
is cast to an integer and as it doesn't contain any numbers the result is0
. Thus you access$str[0]
, which is the first character of the string, which is set.isset() 检查特定变量或数组元素是否存在。包含任何内容(包括
null
或0
)的变量/元素仍然会被设置,因为它存在。如果要检查非零内容,请使用empty()
代替,如果变量存在但包含''
、null,则返回 true
、0
、'0'
等...但是,您仍然需要进行 isset 测试,因为 empty() 会触发“ no such array key”如果您尝试在确实不存在的数组键上运行它,则会发出警告。isset() checks if a particular variable or array element exists. A variable/element that contains ANYTHING, including a
null
or a0
is still set, because it exists. If you want to check for non-zero contents, then useempty()
instead, which will come back true if the variable exists but contains''
,null
,0
,'0'
, etc... However, you'd still need to do an isset test, as empty() will trigger a "no such array key" warning if you attempt to run it on an array key which truly does not exist.