这是检查数组是否有多个元素的更好方法?

时间:2022-10-03 00:09:28

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?

两者中哪一个更好?在这种情况下,我该如何判断两种不同的方法呢?是否有性能检查表可用来测量哪个更好?

10 个解决方案

#1


336  

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']的值,而第二个检查数组中元素的数量。

#2


44  

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确实是一个数组。

sizeof is an alias of count, I prefer to use count because:

sizeof是count的别名,我更喜欢使用count,因为:

  1. 1 less character to type
  2. 1 .字体较少。
  3. sizeof at a quick glance might mean a size of an array in terms of memory, too technical :(
  4. 快速浏览的sizeof可能意味着一个数组大小的内存,太技术了:(

#3


6  

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(),因为sizeOf()只是count()的别名,在许多其他语言中并不相同。许多程序员期望sizeof()来返回分配的内存。

#4


5  

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 Countable object, so you're pretty safe with this.

sizeof()是count()的别名。这两种方法都使用非数组,但是如果参数是数组或可数对象,那么它们只返回大于1的值,因此您非常安全。

#5


5  

Obviously using count($arr) > 1 (sizeof is 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']元素。

#6


5  

Use count()

使用count()

if (count($my_array) > 1) {
// do
}

this page explains it pretty well http://phparraylength.com/

这个页面解释得很好:http://phparraylength.com/。

#7


4  

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';

#8


3  

isset() only checks if a variable is set.. Has got nothing to do with size or what the array contains

isset()只检查变量是否设置。与大小或数组包含的内容无关?

#9


2  

I assume $arr is an array then this is what you are looking for

我假设arr是一个数组,那么这就是你要找的。

if ( sizeof($arr) > 1) ...

#10


1  

The first method if (isset($arr['1'])) will not work on an associative array.

第一个方法如果(isset($arr['1']))将不会在关联数组中工作。

For example, the following code displays "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.";
}

#1


336  

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']的值,而第二个检查数组中元素的数量。

#2


44  

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确实是一个数组。

sizeof is an alias of count, I prefer to use count because:

sizeof是count的别名,我更喜欢使用count,因为:

  1. 1 less character to type
  2. 1 .字体较少。
  3. sizeof at a quick glance might mean a size of an array in terms of memory, too technical :(
  4. 快速浏览的sizeof可能意味着一个数组大小的内存,太技术了:(

#3


6  

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(),因为sizeOf()只是count()的别名,在许多其他语言中并不相同。许多程序员期望sizeof()来返回分配的内存。

#4


5  

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 Countable object, so you're pretty safe with this.

sizeof()是count()的别名。这两种方法都使用非数组,但是如果参数是数组或可数对象,那么它们只返回大于1的值,因此您非常安全。

#5


5  

Obviously using count($arr) > 1 (sizeof is 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']元素。

#6


5  

Use count()

使用count()

if (count($my_array) > 1) {
// do
}

this page explains it pretty well http://phparraylength.com/

这个页面解释得很好:http://phparraylength.com/。

#7


4  

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';

#8


3  

isset() only checks if a variable is set.. Has got nothing to do with size or what the array contains

isset()只检查变量是否设置。与大小或数组包含的内容无关?

#9


2  

I assume $arr is an array then this is what you are looking for

我假设arr是一个数组,那么这就是你要找的。

if ( sizeof($arr) > 1) ...

#10


1  

The first method if (isset($arr['1'])) will not work on an associative array.

第一个方法如果(isset($arr['1']))将不会在关联数组中工作。

For example, the following code displays "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.";
}