sizeof

(PHP 3, PHP 4, PHP 5)

sizeof -- count() 的别名

说明

本函数是该函数的别名:count()


add a note add a note User Contributed Notes
stoobers at yahoo dot com
08-Jul-2006 03:27
here is a significant gotcha!
sizeof($a) gives the size of $a when $a is an array.

but call it on a string and it will return 1.  it won't treat a string like a char array, as the c language does.  to get the size of a string, use strlen.  also, there is no end of string character counted by strlen.
Moslehi[atsign]Gmail[dot]com
18-Mar-2006 07:12
A simple example for numeric values :

<?PHP

function array_average($arr){
$sum = array_sum($arr);
$num = sizeof($arr);
echo
$sum/$num;
}

$myarray = array(1,2,3,4);
array_average($myarray); // displays 2.5 as average of 1,2,3,4

?>

[Arash Moslehi]
blasterncs at hotmail dot com
13-Feb-2006 03:41
In some cases you can use this.

<FORM ACTION="receiver.php" METHOD=POST>
   <INPUT TYPE=CHECKBOX NAME="arreglo[]" value="1">Thing #1
   <INPUT TYPE=CHECKBOX NAME="arreglo[]" value="2">Thing #2
   <INPUT TYPE=CHECKBOX NAME="arreglo[]" value="3">Thing #1
   <INPUT TYPE=CHECKBOX NAME="arreglo[]" value="4">Thing #4
   <INPUT TYPE=SUBMIT>
</FORM>

In the case you mark two (1 & 3) you will have that result:

sizeof($_POST[arreglo]) -> 2

the different is that you can use:
for($i=0; $i<sizeof($_POST[arreglo]); $i++)

Because the array is like that:
$_POST[arreglo]=array( '0'=> '1', '1' => '3');

Another thing.
If you have an array with null values, use this:
count(array_keys($array));
pfortin at ss2i dot ca
07-Jul-2004 11:25
Well, I'm not very sure about the above and it seems a bit overkill if you want my opinion. I prefer to use foreach() or the array_keys function if you only need the selected indexes ... Example :

if(sizeof($_POST['Checkboxes']))
{
           foreach($_POST['Checkboxes'] as $Index => $Value)
           {
                     DoStuffHere();
           }
}

OR

$keys = array_keys($_POST['Checkboxes']);

Using this method, you have the $Index set, which in the above exemple would be the array offset. You can use the functions array_keys() as well which will return the keys of the array, in this case the selected checkboxes.
jdittmer at ppp0 dot net
21-Feb-2004 07:46
This gets pretty near the size of an array:
strlen(implode('', array_values($array) + array_keys($array)).

The internal size is perhaps more like:
strlen(serialize($array).
gramo at skina dot com dot co
26-Mar-2003 01:48
[ Editor's Note: Unchecked boxes are not passed to the form action page at all.  Therefore if only two of the four checkboxes below are checked, only two will be passed to the action page and sizeof/count will return two.  grammo's statement that the array contains NULL elements is incorrect. ]

When you have a php page receiving data from a form with an array like:

--------8<----------------8<----------------8<--------
<FORM ACTION="receiver.php" METHOD=POST>
   <INPUT TYPE=CHECKBOX NAME="arreglo[0]">Thing #1
   <INPUT TYPE=CHECKBOX NAME="arreglo[1]">Thing #2
   <INPUT TYPE=CHECKBOX NAME="arreglo[2]">Thing #1
   <INPUT TYPE=CHECKBOX NAME="arreglo[3]">Thing #4
   <INPUT TYPE=SUBMIT>
</FORM>
--------8<----------------8<----------------8<--------

And you just pick Thing #2 and Thing #4 like:
--------8<----------------8<----------------8<--------
[ ] Thing #1
[X] Thing #2
[ ] Thing #1
[X] Thing #4
[OK]
--------8<----------------8<----------------8<--------

When you count the number of elements in arreglo {NULL, "on", NULL, "on"} you receive just a 2 for answer, because NULL counts as no element for the count function.

These have huge importance when you generate the forwarding form with a database query and you need to know how many elements does the script generate.

The only way to fix these problem is by adding a new element at the form in this way:
--------8<----------------8<----------------8<--------<FORM ACTION="receiver.php" METHOD=POST>
   <INPUT TYPE=HIDDEN NAME="arreglo_size" VALUE="4">
   <INPUT TYPE=CHECKBOX NAME="arreglo[0]">Thing #1
   <INPUT TYPE=CHECKBOX NAME="arreglo[1]">Thing #2
   <INPUT TYPE=CHECKBOX NAME="arreglo[2]">Thing #1
   <INPUT TYPE=CHECKBOX NAME="arreglo[3]">Thing #4
   <INPUT TYPE=SUBMIT>
</FORM>
--------8<----------------8<----------------8<--------