0% found this document useful (0 votes)
11 views

PHP_Array_Functions

The document discusses PHP array functions that allow access and manipulation of arrays, including simple and multi-dimensional arrays. Key functions covered include count(), in_array(), current(), pos(), next(), prev(), and reset(), with examples demonstrating their usage. The document provides syntax and output for each function to illustrate their functionality.

Uploaded by

qedimeliyevaa05
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PPTX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
11 views

PHP_Array_Functions

The document discusses PHP array functions that allow access and manipulation of arrays, including simple and multi-dimensional arrays. Key functions covered include count(), in_array(), current(), pos(), next(), prev(), and reset(), with examples demonstrating their usage. The document provides syntax and output for each function to illustrate their functionality.

Uploaded by

qedimeliyevaa05
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PPTX, PDF, TXT or read online on Scribd
You are on page 1/ 6

PHP Array Functions

PHP array functions allow you to access and


manipulate arrays.

Simple and multi-dimensional arrays are


supported.

Here we discuss most important functions and


its use with examples.

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:

The syntax is: count(array, mode)

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.

The syntax is: in_array(search, array, type)


Search: Required. Specifies the what to search for
Array: Required. Specifies the array to search
Type: Optional. If this parameter is set to TRUE, the in_array() function
searches for the search-string and specific type in the array.
Example:
<?php
$people = array("Peter", "Joe", "Glenn", "Cleveland", 23);
echo in_array("23", $people, TRUE) ? "Match found" : "Not
found";
echo in_array("23", $people) ? "Match found" : "Not found";
?>

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

You might also like