PHP 数组:count 还是 sizeof?
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/3974385/
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
PHP array: count or sizeof?
提问by Ben
To find the number of elements in a PHP $array
, which is faster/better/stronger?
要查找 PHP 中的元素数量$array
,哪个更快/更好/更强?
count($array)
or sizeof($array)
?
count($array)
或者sizeof($array)
?
Edit
编辑
Thanks to Andy Lester, I have refined my question to mean from a multilingual perspective. The manual commenters say
感谢 Andy Lester,我从多语言的角度改进了我的问题。手册评论者说
"[sizeof] does not mean the same in many other languages based on C"
“[sizeof] 在基于 C 的许多其他语言中并不意味着相同”
Is this true?
这是真的?
采纳答案by alex
I would use count()
if they are the same, as in my experience it is more common, and therefore will cause less developers reading your code to say "sizeof()
, what is that?" and having to consult the documentation.
count()
如果它们相同,我会使用,因为根据我的经验,它更常见,因此会导致阅读您的代码的开发人员更少说“sizeof()
那是什么?” 并且必须查阅文档。
I think it means sizeof()
does not work like it does in C (calculating the size of a datatype). It probably made this mention explicitly because PHP is written in C, and provides a lot of identically named wrappers for C functions (strlen()
, printf()
, etc)
我认为这意味着sizeof()
它不像在 C 中那样工作(计算数据类型的大小)。它可能使这个明确提到由于PHP是用C语言编写,并提供了大量具有相同名称的包装为C函数(strlen()
,printf()
,等)
回答by Reza Baradaran Gazorisangi
According to phpbench:
根据phpbench:
Is it worth the effort to calculate the length of the loop in advance?
提前计算循环的长度是否值得?
//pre-calculate the size of array
$size = count($x); //or $size = sizeOf($x);
for ($i=0; $i<$size; $i++) {
//...
}
//don't pre-calculate
for ($i=0; $i<count($x); $i++) { //or $i<sizeOf($x);
//...
}
A loop with 1000 keys with 1 byte values are given.
给出了具有 1 个字节值的 1000 个键的循环。
+---------+----------+
| count() | sizeof() |
+-----------------+---------+----------+
| With precalc | 152 | 212 |
| Without precalc | 70401 | 50644 |
+-----------------+---------+----------+ (time in μs)
So I personally prefer to use count()instead of sizeof()with pre calc.
所以我个人更喜欢在pre calc 中使用count()而不是sizeof()。
回答by Andy Lester
回答by Andy Groff
According to the website, sizeof()
is an alias of count()
, so they should be running the same code. Perhaps sizeof()
has a little bit of overhead because it needs to resolve it to count()
? It should be very minimal though.
根据网站,sizeof()
是 的别名count()
,因此它们应该运行相同的代码。也许sizeof()
有一点开销,因为它需要解决它count()
?虽然它应该是非常小的。
回答by Mehdi Bounya
I know this is old but just wanted to mention that I tried thiswith PHP 7.2:
我知道这是旧的,但只是想提一下我用 PHP 7.2尝试过这个:
<?php
//Creating array with 1 000 000 elements
$a = array();
for ($i = 0; $i < 1000000; ++$i)
{
$a[] = 100;
}
//Measure
$time = time();
for ($i = 0; $i < 1000000000; ++$i)
{
$b = count($a);
}
print("1 000 000 000 iteration of count() took ".(time()-$time)." sec\n");
$time = time();
for ($i = 0; $i < 1000000000; ++$i)
{
$b = sizeof($a);
}
print("1 000 000 000 iteration of sizeof() took ".(time()-$time)." sec\n");
?>
and the result was:
结果是:
1 000 000 000 iteration of count() took 414 sec
1 000 000 000 iteration of sizeof() took 1369 sec
So just use count()
.
所以只需使用count()
.
回答by Mina Ragaie
sizeof()
is just an alias of count()
as mentioned here
sizeof()
只是count()
这里提到的别名
回答by Harish rana
Please use count function, Here is a example how to count array in a element
请使用计数功能,这是如何计算元素中的数组的示例
$cars = array("Volvo","BMW","Toyota");
echo count($cars);
The count()
function returns the number of elements in an array.
该count()
函数返回数组中元素的数量。
The sizeof()
function returns the number of elements in an array.
该sizeof()
函数返回数组中元素的数量。
The sizeof()
function is an alias of the count()
function.
该sizeof()
功能是的别名count()
功能。