SPL与阵列:我们什么时候应该使用SPL和我们何时应该在PHP中使用数组? [英] SPL vs. Array: When should we use SPL and when should we use Array in PHP?

查看:126
本文介绍了SPL与阵列:我们什么时候应该使用SPL和我们何时应该在PHP中使用数组?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

在Java和C ++的时候,我们不知道的大小 - 没有使用像PHP数组,而不是用链表等

In java and C++ when we don't know the size - array not used like in PHP, instead used linkedList etc.

在PHP中存在的SPL,但大多数时候程序员使用数组,为什么?(因为人们不知道SPL)?

In PHP exist SPL, but most of the times programmers use array, why (because people don't know about SPL )?

当我们要在PHP和whenSPL使用数组和是在PHP和Java / C ++?

When we should use Array in PHP and whenSPL and what is the difference in this case between PHP and Java/C++?

推荐答案

请求后,他们被释放每一个PHP请求必须初始化所有变量和。由于不是常来的地方特殊的数据结构(如maxheap,链表或队列)比数组更有效率的情况。
此外阵列更易于理解和使用的初学者。

Every PHP request must initialize all variables and after request they are freed. Because of that not often comes situations where special data structures (like maxheap, linkedlist or queue) are more efficient than array. Also arrays are much simpler to understand and use for beginner.

这是C ++的差异PHP是数组的长度是动态的。只要你想,你可以添加元素。

Difference from C++ in PHP is that arrays length is dynamic. You can add elements whenever you want.

$arr=array();
$arr[]=5; //add integer to array
echo count($arr); //1
$arr[]=7;
echo count($arr); //2

您可以动态创建并添加阵列到另一个阵列

you can dynamically create and add array to another array

$arr[]=array();
$arr[2][]=5;
echo count($arr); //3
echo count($arr[2]); //1

这将创造新的数组,价值5元加并将其添加为元素数组$编曲。

This will create new array, add element with value 5 and add it as element to array $arr.

$arr[][]=5;

在PHP中的数组是哈希表,所以你可以不仅整数键也是字符串:

In PHP arrays are hash tables, so you can have not only integer keys but also strings:

$arr['somekey']='somevalue';

如果数组元素是整数,那么每个元素需要一个值结构(zval的),这需要16个字节。还需要一个散列桶 - 这需要36个字节。这给了每个值52字节。内存分配头采取另8个字节* 2 - 这给68个字节

If array element is integer then each element requires a value structure (zval) which takes 16 bytes. Also requires a hash bucket - which takes 36 bytes. That gives 52 bytes per value. Memory allocation headers take another 8 bytes*2 - which gives 68 bytes.

关于PHP数组: http://oreilly.com/catalog/progphp/chapter /ch05.html

这篇关于SPL与阵列:我们什么时候应该使用SPL和我们何时应该在PHP中使用数组?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆