首页  >  文章  >  后端开发  >  PHP 数组长度

PHP 数组长度

PHPz
PHPz原创
2024-08-29 12:44:04232浏览

PHP 数组长度被定义为一个数组,用于获取其中的许多元素。使用 count() 函数和 size(),我们可以检索元素的计数。数组包含字符串或整数值,可以是单维或多维的。该数组用于保存键值对中的值,这是一个索引数组,具有许多用于数组处理的内置函数。此处使用两个 Right 函数(预定义)可以节省大量计算值的时间。

广告 该类别中的热门课程 PHP 开发人员 - 专业化 | 8 门课程系列 | 3次模拟测试

开始您的免费软件开发课程

网络开发、编程语言、软件测试及其他

语法

PHP 数组长度定义如下:

Count(array name, mode);

该函数有两个参数,即数组名称,后跟表示维度的模式。空数组表示零,对于非数组值返回“1”。

数组长度在 PHP 中如何工作?

数组长度由函数的数量和大小决定。根据语法,可选的 mode 参数被递归地设置为 count(),这将递归地计算数组中的元素数量。该函数在多维数组中大量使用。

要知道数组长度,我们有几个常见的原因:

  • 使用“for”循环在元素之间移动。
  • 没有返回任何搜索元素。
  • 计算数组中的平均值。

但是在 PHP 中,为了获取数组中元素的数量,这里启用了 sizeof 或 count 函数来预测 PHP 中的数组长度。由于我们的代码中的元素数量根据用户需求而变化,因此了解数组列表的实际长度非常重要。 PHP 有两个内置函数,即 count 和 size of。

使用count():对元素进行计数。

我们可以这样使用:

代码:

$a1=array(6,3,1,9);
echo " The size is given as =", count($a1);

因此,在这里,计数函数返回对象中的元素数量,并简单地在关联数组中进行计数。在上面的示例代码中,我们使用了一维数组。我们使用了 PHP 的原生函数 count,所以当我们执行上面的代码片段时,函数的输出是“4”。这就是我们获取值的方式。

第二种情况是当我们使用参数模式对元素进行计数时,为了执行此操作,我们传递了一个常量“recursive”作为参数来对元素进行计数。在这种情况下,数组的长度的确定方式不同。

代码:

$avar = array (2,6,7, array (19,18,60));
$nelem = count ($avar, COUNT_RECURSIVE);
echo $nelem;

上面的代码将输出显示为“7”而不是值“6”。

要对数组元素进行迭代,我们可以使用for循环进行迭代。这些值应该循环继续执行。因此,在每次迭代步骤中,值都会增加 1。在 count() 方法中使用 for 循环时应小心,因为 PHP 缺乏区分索引数组和关联数组。但大多数程序员开发人员假装使用 count() 而不是 sizeof(),因为它返回内存大小。尽管它与 count() 函数类似,但大多数人还是坚持使用 count() 函数。

PHP 数组长度示例

有两种方法定义 PHP 数组长度或大小计数。让我们在下面的示例中看看这些方法是如何确定长度的。

示例#1

创建一个简单的数组来计算元素的数量。

代码:

<?php
$flowers= ['Jasmine', 'Diasy', 'Rose'];
echo "The count is: " . count($flowers);
?>

说明:

  • 当我们执行上述代码片段时,输出显示为“3”,因为数组元素有 3 个元素。
  • 首先,我们创建了一个“花”数组,在下一行中,我们使用了 count 命令。

输出:

PHP 数组长度

示例#2

代码:

<?php
$program = [
'C++' => ['Polymorphism', 'Inheritance', 'Template'],
'Java' => ['Interface', 'Multithread', 'Exception'],
'PHP' => ['ArrayLength', 'Count']
];
echo "No. of count: ". count($program)."<br>";
echo "Multidimensional count: ". count ($program, 1);
?>

说明:

  • 确定计数为“1”的数组长度。

输出:

PHP 数组长度

示例#3

代码:

<!DOCTYPE html>
<html>
<body>
<?php
$bike=array
(
"Hero Splender"=>array
(
"HP2345",
"HS3456"
),
"Royal Enfield"=>array
(
"R3",
"Tr5"
),
"Honda Activa 6G"=>array
(
"Classic 250"
)
);
echo "General count: " . sizeof($bike)."<br>";
echo "Recursive Number: " . sizeof($bike,1);
?>
</body>
</html>

说明:

  • 上面的代码确定了 General 计数为‘3’,递归模式的数组长度为‘8’。

输出:

PHP 数组长度

示例#4

使用 For 循环。

代码:

<?php
$arr_iter = array (26,60,70,10,130,67);
echo "No of elements in the array = ", sizeof($arr_iter), "<br /><br />";
//Iterating through the array
for ($k=0; $k <sizeof($arr_iter); $k++){
echo "List of elements are: $arr_iter[$k] <br />";
}
?>

说明:

  • The above code takes the length of an array using the sizeof function, and the array elements are iterated using for-loop.
  • The output shows the list of values in an array in each iteration.

Output:

PHP 数组长度

Example #5

Using Null value in mode.

Code:

<?php
$m[0] = 2;
$m[1] = 6;
$m[2] = 8;
value_res(count($m));
$n[3]  = 1;
$n[4]  = 3;
$n[8] = 5;
value_res(count($n));
value_res(count(null));
value_res(count(false));
?>

Explanation:

  • The above code returns a parameter array as the value is assigned as null. So the output looks like this.

Output:

PHP 数组长度

Example #6

Array length Using 2D array.

Code:

<?php
$foods = array('choclates' => array('Diary Milk', 'Cadbury Godiva', 'Nestle','Snikkers',
'Candy Craze'), 'Fast Food' => array('Nuggets', 'Salad Platters'));
echo count($foods, 1);
echo "</br>";
echo sizeof($foods, 1);
?>

Explanation:

  • In the above code, we have used mode count as ‘1’; therefore, this multi-dimensional array counts the value as ‘9’.

Output:

PHP 数组长度

Example #7

Word Count.

Code:

<?Php
$stringtype=' This is EDUCBA Asia largest Web Learning Platform providing courses in various Domains. We Provide Certification from many Leading Universities across the globe.';
$my1_array=explode(" ",$stringtype);
echo "No.Of words in the String = ".sizeof($my1_array);
?>

Explanation:

  • The above program Stores a paragraph in a variable of type String.
  • Here an array is created using explode function to split the array.
  • Finally, count the number of words in a paragraph.
  • We get the count as below.

Output:

PHP 数组长度

Conclusion

Here we have seen how to determine the length or size of an array in PHP and also the various methods to show how PHP functions are used to take memory size used by the array. There is no difference between the count and size of the function. Depends upon the developer, the methods are picked while writing the code. In this article, we explored PHP’s array length with many examples, and also, we have also seen more about multi-dimensional arrays.

以上是PHP 数组长度的详细内容。更多信息请关注PHP中文网其他相关文章!

声明:
本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系admin@php.cn