★ wanayoo — archive 1999 http://www.php.net/manual/de/function.sizeof.phpNouvelle recherche | Portail wanayoo

PHP Home Page

Manual Table of Contents
Up to Arrays
Quick Reference
English version of this pageJapanese version of this pageItalian version of this pageFrench version of this pageHungarian version of this page
Arrays
* array
* array_count_values
* array_flip
* array_keys
* array_merge
* array_merge_recursive
* array_multisort
* array_pad
* array_pop
* array_push
* array_rand
* array_reverse
* array_shift
* array_slice
* array_splice
* array_unshift
* array_values
* array_walk
* arsort
* asort
* compact
* count
* current
* each
* end
* extract
* in_array
* key
* krsort
* ksort
* list
* next
* pos
* prev
* range
* reset
* rsort
* shuffle
* sizeof
* sort
* uasort
* uksort
* usort
Manual: sizeof
View the source code for this pageSearch the site



Previous page
 shuffle
 Updated
Sun, 06 Aug 2000
sort 
Next page


sizeof

(PHP3 , PHP4 )

sizeof -- Get the number of elements in an array

Description

int sizeof (array array)

Returns the number of elements in the array.

See also: count()


User Contributed Notes: sizeof


HSH@taxon.demon.nl
25-Jun-1999 04:09
If you're used to 'true' arrays in other languages, this function may trip you up: it returns the number of elements in the array that is actually filled.

If only myarray[2] has a value and mayarray[0] and myarray[1] do not, then <B>sizeof(mayarray)</B> will return <B>1</B>, not 3.



This is caused by the fact that arrays here are actually always associative arrays. If you're filling some elements of the array using an index counter, you may want to save the counter value for the highest element to access the array later on.



HSH@taxon.demon.nl
25-Jun-1999 04:18
...and BTW: <B>count()</B> does the same thing: it also returns only the number of actually <I>filled</I>elements.


christian@schmidt.net
08-Dec-1999 10:50
Note that sizeof($x), where $x is a variable that is set but is not an array sizeof, returns 1. Hence you have to be careful e.g. when you are dealing with arrays of multiple dimensions.

for ($i = 0; $i < sizeof($myarray[$j]); $i++) will not work as expected if $myarray[$j] hasn't been assigned a value (i.e. an array value).



sgarner@expio.co.nz
10-Jan-2000 09:22
If some elements in your array are not set, then sizeof() and count() will not return the index of the last element, but will return the number of set elements. To find the index of the last element in the array:

<pre>
end($yourArray);
$index = key($yourArray);
</pre>

... Where $yourArray is the array you want to find the last index ($index) of.



cu@ulakbim.gov.tr
04-Feb-2000 09:12
Remember, PHP code is interpreted so sizeof() is always evaluated in place and everytime. So a very simple code like below can blow up your server box (tested :)

<PRE>
&lt;?
$arr= range(0,19);
echo sizeof($arr)."
";
for($i=0; $i<=sizeof($arr); $i++) {
// <= is used intentionally
$arr[$i] = "";
echo sizeof($arr)." Passing over #".$i."
\n";
}
?>
</PRE>

The assignment $arr[$i] when used in conjunction with <= in the for loop causes the assignment to add an extra element to the end of the array. So when the for-condition is next time checked when $i=20, sizeof($arr) will yield 21, the next time 22, and so on. So the program in fact starts to leak heavily and very rapidly.
You can instead use:
<PRE>
&lt;?
//...
$c = sizeof($arr);
for($i = 0; $i <= $c; $i++) {
// <= is used intentionally again ...
?>
</PRE>
This one will add the 21th element, but will stop then. No leak at all...



james@totalgeek.org
19-May-2000 09:13
Once again a user post on php.net saved my butt!

I was doing exactly what the comment above said not to do. :)



 About Notes


Previous page
 shuffle
 Updated
Sun, 06 Aug 2000
sort 
Next page





Who's responsible for this?
Top of this page

Site
Hosting:



Located in
United States
Elements of this website are subject to copyright.
Questions about installing or using PHP should be directed to one of the mailing lists.
Only questions about the website should be directed to webmaster@php.net.