php 哪个是检查数组是否有多个元素的更好方法?
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/10024910/
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
Which is a better way to check if an array has more than one element?
提问by tuxnani
I just need to check if an array has more than one element. I am trying to do it this way :
我只需要检查一个数组是否有多个元素。我正在尝试这样做:
if (isset($arr['1']))
the other traditional way is
另一种传统方式是
if (sizeof($arr)>1)
Which of the two is better? In such situaions, how should I judge between two alternate methods? Is there any performance check meter available to measure which is better?
两者哪个更好?在这种情况下,我应该如何判断两种替代方法?是否有任何性能检查表可用于衡量哪个更好?
回答by Chibuzo
Use this
用这个
if (sizeof($arr) > 1) {
....
}
Or
或者
if (count($arr) > 1) {
....
}
sizeof()is an alias for count(), they work the same.
sizeof()是 的别名count(),它们的工作方式相同。
Edit:Answering the second part of the question:
The two lines of codes in the question are not alternative methods, they perform different functions. The first checks if the value at $arr['1']is set, while the second returns the number of elements in the array.
编辑:回答问题的第二部分:问题中的两行代码不是替代方法,它们执行不同的功能。第一个检查是否$arr['1']设置了值 at ,而第二个返回数组中的元素数。
回答by Andreas Wong
if(is_array($arr) && count($arr) > 1)
if(is_array($arr) && count($arr) > 1)
Just to be sure that $arr is indeed an array.
只是为了确保 $arr 确实是一个数组。
sizeofis an alias of count, I prefer to use count because:
sizeof是 的别名count,我更喜欢使用计数,因为:
- 1 less character to type
- sizeof at a quick glance might mean a size of an array in terms of memory, too technical :(
- 要输入的字符少 1 个
- sizeof 一目了然可能意味着就内存而言数组的大小,太技术性了:(
回答by Fahmi
Use count()
用 count()
if (count($my_array) > 1) {
// do
}
this page explains it pretty well http://phparraylength.com/
这个页面很好地解释了它http://phparraylength.com/
回答by Alix Axel
if (count($arr) >= 2)
{
// array has at least 2 elements
}
sizeof()is an alias for count(). Both work with non-arrays too, but they will only return values greater than 1 if the argument is either an array or a Countableobject, so you're pretty safe with this.
sizeof()是 的别名count()。两者也适用于非数组,但如果参数是数组或Countable对象,它们只会返回大于 1 的值,因此您对此非常安全。
回答by ThiefMaster
Obviously using count($arr) > 1(sizeofis just an alias for count) is the best solution.
Depending on the structure of your array, there might be tons of elements but no $array['1']element.
显然,使用count($arr) > 1(sizeof只是 ) 的别名count是最好的解决方案。根据数组的结构,可能有大量元素但没有$array['1']元素。
回答by Mithun Debnath
I prefer the count()function instead of sizeOf()as sizeOf()is only an alias of count()and does not mean the same in many other languages. Many programmers expect sizeof()to return the amount of memory allocated.
我更喜欢count()函数而不是sizeOf()assizeOf()只是一个别名,count()在许多其他语言中并不意味着相同。许多程序员希望sizeof()返回分配的内存量。
回答by Kabir Hossain
For checking an array empty() is better than sizeof().
对于检查数组 empty() 比 sizeof() 更好。
If the array contains huge amount of data. It will takes more times for counting the size of the array. But checking empty is always easy.
如果数组包含大量数据。计算数组的大小需要更多时间。但是检查空总是很容易的。
//for empty
if(!empty($array))
echo 'Data exist';
else
echo 'No data';
//for sizeof
if(sizeof($array)>1)
echo 'Data exist';
else
echo 'No data';
回答by Bono
isset() only checks if a variable is set.. Has got nothing to do with size or what the array contains
isset() 只检查是否设置了变量。与大小或数组包含的内容无关
回答by Thariama
I assume $arr is an array then this is what you are looking for
我假设 $arr 是一个数组,那么这就是你要找的
if ( sizeof($arr) > 1) ...
回答by Dan Cron
The first method
if (isset($arr['1']))will not work on an associative array.
第一种方法
if (isset($arr['1']))不适用于关联数组。
For example, the following code displays "Nope, not more than one."
例如,以下代码显示“Nope, not more than one”。
$arr = array(
'a' => 'apple',
'b' => 'banana',
);
if (isset($arr['1'])) {
echo "Yup, more than one.";
} else {
echo "Nope, not more than one.";
}

