Skip to content

PHP Array Functions: count() and sizeof()

count(): Used to find the number of elements in the array.

sizeof(): Used to find the number of elements in the array.

COUNT_RECURSIVE: In multidimensional arrays, elements within all dimensions are included in the counting process.


First, define and print an array.

	$ARRAY_LIST = ["maria","alex","jason","bob"];
	
	echo "<pre>";
	print_r(ARRAY_LIST);
	echo "</pre>";

Array  (      
[0] => maria      
[1] => alex      
[2] => jason      
[3] => bob  
)

Then use the count () function.

	$ARRAY_LIST = ["maria","alex","jason","bob"];
	$ARRAY_LIST_CURRENT = current($ARRAY_LIST);	

	echo "<pre>";
	print_r($ARRAY_LIST_CURRENT);
	echo "</pre>";

4

See the following examples.


Example 1

	$ARRAY_LIST = ["maria","alex","jason","bob"];
	$ARRAY_LIST_SIZEOF = sizeof($ARRAY_LIST);

	echo "<pre>";
	print_r($ARRAY_LIST_SIZEOF);
	echo "</pre>";
	

4


Example 2

	$ARRAY_LIST = ["maria","alex","jason",array("jacop","bella","harry"),"bob"];
	$ARRAY_LIST_SIZEOF = sizeof($ARRAY_LIST);

	echo "<pre>";
	print_r($ARRAY_LIST_SIZEOF);
	echo "</pre>";

5


Example 3

	$ARRAY_LIST = ["maria","alex","jason",array("jacop","bella","harry"),"bob"];
	$ARRAY_LIST_SIZEOF = sizeof($ARRAY_LIST,COUNT_RECURSIVE);

	echo "<pre>";
	print_r($ARRAY_LIST_SIZEOF);
	echo "</pre>";

8

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out /  Change )

Google photo

You are commenting using your Google account. Log Out /  Change )

Twitter picture

You are commenting using your Twitter account. Log Out /  Change )

Facebook photo

You are commenting using your Facebook account. Log Out /  Change )

Connecting to %s

This site uses Akismet to reduce spam. Learn how your comment data is processed.

%d bloggers like this: