PHP - 空数组
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/5421457/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
PHP - empty array
提问by Alex
(array)$someemptyvariablethatisnotarray
returns array([0] =>)
instead of array()
(array)$someemptyvariablethatisnotarray
返回array([0] =>)
而不是array()
How can I make it so I get a empty array that is not iterated when I'm using it inside foreach() ?
我怎样才能得到一个空数组,当我在 foreach() 中使用它时,它不会被迭代?
回答by trickwallett
$var = array();
will empty an array. Is this what you're after?
将清空一个数组。这是你追求的吗?
回答by powtac
The feature which you are using, is called "casting". This means a variable is forced to become a given type, in your example an array. How the var is converted is not always obvious in PHP!
您正在使用的功能称为“铸造”。这意味着变量被迫成为给定类型,在您的示例中为数组。在 PHP 中如何转换 var 并不总是很明显!
In your example $someemptyvariablethatisnotarray becomes an array with one entry with a NULL value.
在您的示例中, $someemptyvariablethatisnotarray 成为一个数组,其中一个条目为 NULL 值。
The PHP documentationsays:
在PHP文件说:
The behaviour of an automatic conversion to array is currently undefined.
自动转换为数组的行为目前未定义。
To solve your code I would recommend something like this:
为了解决您的代码,我建议您这样做:
if (!is_array($someemptyvariablethatisnotarray) {
$someemptyvariablethatisnotarray = array();
}
回答by sharpner
if(!$variable){
return array();
}
回答by Brad Christie
$somevar = empty($somevar) ? array() : (array)$somevar;
Maybe? Though I'm not sure I get the cast, or the purpose. Care to ellaborate a bit more (maybe an example of what you're trying to accomplish?)
也许?虽然我不确定我是否得到了演员或目的。小心详细说明一点(也许是你想要完成的一个例子?)
回答by beta0x64
Try unset($someemptyvariablethatisnotarray[0])
:)
试试unset($someemptyvariablethatisnotarray[0])
:)
回答by fsodano
how are you?
你好吗?
I believe this is what you're after:
我相信这就是你所追求的:
$something = false;
foreach((array)(empty($something) ? null : $something) as $k){
echo 'never enters here';
}
You don't get an empty array, because when you set "(array)false", it means you'll have a single element, and that element will have the "FALSE" value assigned to it.
您不会得到一个空数组,因为当您设置“(array)false”时,这意味着您将拥有一个元素,并且该元素将分配给它的“FALSE”值。
Same happens with an empty string (not a null one!)(array)$emptystring will return an array which contains a single element, which is an empty string!
空字符串也会发生同样的情况(不是空字符串!)(array)$emptystring 将返回一个包含单个元素的数组,这是一个空字符串!
Similar to doing:
类似于做:
array('');
Hope it helps.
希望能帮助到你。
Cheers!
干杯!
回答by Rocket Hazmat
When you cast a non-array as an array, it creates an array with that variable as the only value.
当您将非数组转换为数组时,它会创建一个以该变量作为唯一值的数组。
If you want an empty array, you need to return array()
.
如果你想要一个空数组,你需要返回array()
.
回答by james
just use count()
, for example: if(count($array) == 0 ){ // empty array }
只需使用count()
,例如:if(count($array) == 0 ){ // empty array }