PHP Array Functions
count() Function
Definition and Usage
The count() function returns the number of elements in an array.
This Function Return True if the elememt is in array otherwise it will return False.
Note!
Example:-In Ecommerce(To show how many Item is Remaining)
Syntax
| Parameter |
Description |
| array |
Required. Specifies the array |
| mode |
Optional. Specifies the mode. Possible values:
- 0 - Default. Does not count all elements of multidimensional arrays
- 1 - Counts the array recursively (counts all the elements of multidimensional arrays)
|
Example 1
Count the array recursively::
<?php
$cars=array("Volvo"=>array ( "XC60","XC90"),
"BMW"=>array( "X3", "X5" ),
"Toyota"=>array (
"Highlander" ));
echo "Normal count: " . count($cars)."<br>";
echo "Recursive count: " . count($cars,1);
?>
Run Example>>
Example 2
Return the number of elements in an array:
<?php
$name = array("Falguni","Shital","Keyur","Daksh");
echo "Total Value is :" .count($name);
?>
Run Example>>
sizeof() Function
Definition and Usage
The sizeof() function returns the number of elements in an array.
The sizeof() function is an alias of the count() function.
Note!
Example:-In Ecommerce(To show how many Item is Remaining)
Syntax
| Parameter |
Description |
| array |
Required. Specifies the array |
| mode |
Optional. Specifies the mode. Possible values:
- 0 - Default. Does not count all elements of multidimensional arrays
- 1 - Counts the array recursively (counts all the elements of multidimensional arrays)
|
Example 1
Count the array recursively::
<?php
$cars=array("Volvo"=>array ( "XC60","XC90"),
"BMW"=>array( "X3", "X5" ),
"Toyota"=>array (
"Highlander" ));
echo "Normal count: " . sizeof($cars)."<br>";
echo "Recursive count: " . sizeof($cars,1);
?>
Run Example>>
Example 2
Return the number of elements in an array:
<?php
$name = array("Falguni","Shital","Keyur","Daksh");
echo "Total Value is :" .sizeof($name);
?>
Run Example>>