bat批处理中使用数组示例_在PHP教程中使用示例计算数组长度

bat批处理中使用数组示例

bat批处理中使用数组示例

PHP programming language provides the count(), sizeof() functions and foreach in order to count the elements in an array. The array can be a single dimension or multidimensional array.

PHP编程语言提供了count()sizeof()函数和foreach以便对数组中的元素进行计数。 该数组可以是一维或多维数组。

count()函数语法 (count() Function Syntax)

The count() function is the most popular way to count array length.

count()函数是最常用的计算数组长度的方法。

count( ARRAY , MODE )
  • `ARRAY` is the array we want to count which can be a simple array or multidimensional array

    “ ARRAY”是我们要计数的数组,可以是简单数组或多维数组
  • `MODE` is the count operating mode which can be default which is represented as `0` or recursive `1`. MODE parameter is optional and if it is not provided it will be assumed as 0 by default.

    “ MODE”是计数操作模式,可以默认使用“ 0”或递归“ 1”表示。 MODE参数是可选的,如果未提供,则默认为0。

带count()函数的数组长度 (Array Length with count() Function)

We can use count() function by providing the array we want to get the length. The array is a simple array that provides the car numbers like below.

我们可以通过提供要获取长度的数组来使用count()函数。 该数组是一个简单的数组,提供如下所示的车号。

$weekdays = array ('monday','thuesday','wednesday','thursday','friday','saturday','sunday');

$food = array('fruits' => array('orange', 'banana', 'apple'), 
              'veggie' => array('carrot', 'collard', 'pea'));

echo count($food);
 
echo count($weekdays);
Array Length with count() Function
Array Length with count() Function
带count()函数的数组长度

As we can see the $food array has only two elements in the first level. In the second level, there are more elements. So simple usage of count() function will return 2 as element count as it is not recursive by default.

如我们所见, $food数组在第一级中只有两个元素。 在第二级中,还有更多元素。 因此count()函数的简单用法将返回2作为元素计数,因为默认情况下它不是递归的。

具有count()函数的递归/多维数组长度 (Recursive/Multidimensional Array Length with count() Function)

If the given array is multidimensional we have used some extra parameter with the count() function. We will provide the 1 as a second parameter to the count() function.

如果给定的数组是多维的,则我们在count()函数中使用了一些额外的参数。 我们将把1作为第二个参数提供给count()函数。

$weekdays = array ('monday','thuesday','wednesday','thursday','friday','saturday','sunday');

$food = array('fruits' => array('orange', 'banana', 'apple'), 
'veggie' => array('carrot', 'collard', 'pea'));

echo count($food,1);

echo count($weekdays,1);
Recursive/Multidimensional Array Length with count() Function
Recursive/Multidimensional Array Length with count() Function
具有count()函数的递归/多维数组长度

We can see the by providing the recursive counting the count of the $food array is printed as 8 .

通过提供递归计数,我们可以看到$food数组的计数显示为8。

具有sizeof()函数的数组长度 (Array Length with sizeof() Function)

sizeof() function is used to count the array elements. Actually sizeof() function is an alias of the count() function. It is created in the old days of the PHP where similar to the C programming language sizeof() function. It is preserved as backward compatibility but using sizeof() function is not a good practice especially for the feature.

sizeof()函数用于计算数组元素。 实际上,sizeof()函数是count()函数的别名。 它是在PHP的早期创建的,类似于C编程语言sizeof()函数。 它保留为向后兼容,但使用sizeof()函数并不是一个好的做法,尤其是对于该功能。

$weekdays = array ('monday','thuesday','wednesday','thursday','friday','saturday','sunday'); 

$food = array('fruits' => array('orange', 'banana', 'apple'), 

              'veggie' => array('carrot', 'collard', 'pea')); 

echo sizeof($food); 

echo sizeof($weekdays);
Array Length with sizeof() Function
Array Length with sizeof() Function
具有sizeof()函数的数组长度

We can see from the output that sizeof() function does not count multidimensional arrays 2 level arrays. The size of the $food array is printed as 2.

从输出中我们可以看到sizeof()函数不计算多维数组2级数组。 $ food数组的大小显示为2。

带foreach语句的数组长度 (Array Length with foreach statement)

foreach is an enumeration statement used to enumerate the given array or list. We can also use foreach in order to count given array but this will require some effort if the array is multidimensional.

foreach是用于枚举给定数组或列表的枚举语句。 我们也可以使用foreach来计数给定的数组,但是如果数组是多维的,这将需要一些努力。

$weekdays = array ('monday','thuesday','wednesday','thursday','friday','saturday','sunday'); 

$food = array('fruits' => array('orange', 'banana', 'apple'), 

              'veggie' => array('carrot', 'collard', 'pea')); 

$weekdays_count=0; 

$food_count=0;


foreach($weekdays as $key=>$value){
   $weekdays_count++;
}


echo $weekdays_count;


foreach($food as $key=>$value){ 
   $food_count++; 
}

echo $food_count;
Array Length with foreach statement
Array Length with foreach statement
带foreach语句的数组长度

翻译自: https://www.poftut.com/count-array-length-in-php-tutorial-with-examples/

bat批处理中使用数组示例

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值