In java and C++ when we don't know the size - array not used like in PHP, instead used linkedList etc.
In PHP exist SPL, but most of the times programmers use array, why (because people don't know about SPL )?
When we should use Array in PHP and whenSPL and what is the difference in this case between PHP and Java/C++?
Advantage of PHP Array Less Code: We don't need to define multiple variables. Easy to traverse: By the help of single loop, we can traverse all the elements of an array. Sorting: We can sort the elements of array.
In PHP, there are three types of arrays: Indexed arrays - Arrays with a numeric index. Associative arrays - Arrays with named keys. Multidimensional arrays - Arrays containing one or more arrays.
An array in PHP is actually an ordered map. A map is a type that associates values to keys. This type is optimized for several different uses; it can be treated as an array, list (vector), hash table (an implementation of a map), dictionary, collection, stack, queue, and probably more.
PHP indexed array is an array which is represented by an index number by default. All elements of array are represented by an index number which starts from 0. PHP indexed array can store numbers, strings or any object. PHP indexed array is also known as numeric array.
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.
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
This will create new array, add element with value 5 and add it as element to array $arr.
$arr[][]=5;
In PHP arrays are hash tables, so you can have not only integer keys but also strings:
$arr['somekey']='somevalue';
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.
About arrays in PHP: http://oreilly.com/catalog/progphp/chapter/ch05.html
You're asking us to compare two massively different things, almost the only similarity being that they're both (arrays and the SPL) available in PHP.
To that end, it would be essentially nonsensical to compare directly, or prescribe, when one should be used over the other for times when both might be used to accomplish a task. On that note, both might be used intertwined: for example, using the ArrayIterator
to iterate over an array, or the ArrayObject
to make use of array-style syntax when working with objects.
You also seem to be confused, or just unclear, about what the SPL is; it certainly is not restricted to tools used to iterate over collections of things. Did you mean to ask about specific parts of the library, or are you perhaps just unclear what is available in it?
Use standard arrays, it is faster than ArrayObject.
Use ArrayObject only to implement your own specified arrays with your custom methods.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With