PHP_Array_Functions
PHP_Array_Functions
1.count()
2.current()
3.pos()
4.next()
5.prev()
6.reset()
7.in_array()
1. count: It is used to return the number of
elements in an array:
Example:
<!doctype <!DOCTYPE html>
<html>
<head>
<title>string function</title>
</head>
<body>
<?php
$cars=array("Volvo","BMW","Toyota");
echo count($cars);
?>
</body>
</html>
Output: 3
count with mode:
The syntax is: count(array, mode)
mode: 0 (false) - Default. Does not count all elements of
multidimensional arrays
1 (true) - Counts the array recursively (counts all the
elements of multidimensional arrays)
Example:
<?php
$cars=array("Volvo"=>array("XC60","XC90"),"BMW"=>array("X3","
X5"));
echo "Normal count: " . count($cars)."<br>";
echo "Recursive count: " . count($cars, 1);
?>
Output:
Normal count: 2
Recursive count: 6
2. in_array():
The in_array() function searches an array for a specific value.
Output:
Not found
Match found
1.current() - Returns the current element in an array.
2.pos() - Alias of current(). ↑
3.next() - Advance the internal array pointer of an
array.
4.prev() - Rewinds the internal array pointer.
5.reset() - Sets the internal pointer of an array to its
<?php
first element.
$arr = array("Peter", "Joe", "Glenn", "Cleveland");
echo current($arr) . ", ";
echo pos($arr) . ", ";
echo next($arr) . ", ";
echo next($arr) . ", ";
echo prev($arr) . ", ";
echo reset($arr) . ", ";
echo pos($arr);
?>
Output: Peter, Peter, Joe, Glenn, Joe, Peter, Peter