简体   繁体   English

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

[英]Which is a better way to check if an array has more than one element?

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?是否有任何性能检查表可用于衡量哪个更好?

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']处的值,而第二个返回数组中的元素数。

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: sizeofcount的别名,我更喜欢使用 count 因为:

  1. 1 less character to type少输入 1 个字符
  2. sizeof at a quick glance might mean a size of an array in terms of memory, too technical:( sizeof 乍一看可能意味着数组的大小为 memory,太技术性了:(
if (count($arr) >= 2)
{
  // array has at least 2 elements
}

sizeof() is an alias for count() . sizeof()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.两者都适用于非数组,但如果参数是数组或Countable object ,它们只会返回大于 1 的值,因此您可以放心使用。

Obviously using count($arr) > 1 ( sizeof is just an alias for count ) is the best solution.显然使用count($arr) > 1sizeof只是count的别名)是最好的解决方案。 Depending on the structure of your array, there might be tons of elements but no $array['1'] element.根据数组的结构,可能有大量元素但没有$array['1']元素。

Use count()使用count()

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

this page explains it pretty well http://phparraylength.com/这个页面解释得很好http://phparraylength.com/

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.我更喜欢count() function 而不是sizeOf()因为sizeOf()只是count()的别名,在许多其他语言中并不意味着相同。 Many programmers expect sizeof() to return the amount of memory allocated.许多程序员期望sizeof()返回分配的 memory 的数量。

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

isset() only checks if a variable is set.. Has got nothing to do with size or what the array contains isset() 仅检查是否设置了变量。与大小或数组包含的内容无关

I assume $arr is an array then this is what you are looking for我假设 $arr 是一个数组,那么这就是你要找的

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

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."例如,以下代码显示“不,不超过一个”。

$arr = array(
    'a' => 'apple',
    'b' => 'banana',
);

if (isset($arr['1'])) {
    echo "Yup, more than one.";
} else {
    echo "Nope, not more than one.";
}

I do my array looping and getting filled defaults accordingly in Swift 4/5我在 Swift 4/5 中进行数组循环并相应地填充默认值

   for index in 0..<3
    {
        let isIndexValid = allObjects.indices.contains(index)
        var yourObject:Class = Class()
        if isIndexValid { yourObject = allObjects[index]}
        resultArray.append(yourObject)
    }

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM