How to get the index of the current array element position using a key() function in PHP?

PHP key() function Example: Here, we will figure out how to get the file of a component in a cluster which is right now pointing by the interior pointer?

To get the current pointing component’s record, we utilize the key() function.

the key() function is a library (predefined) function in PHP, which is utilized to restore the record of the current component in a cluster which is pointing.

Note: By default, a record of the current position is 0 (Zero).

Right now, are proclaiming an exhibit with a portion of the strings and printing the file utilizing the key() function. Here, we are additionally utilizing straightaway() function – that will move the pointer to the next cluster component.

Example:

<?php
	//array elements
	$arr = array("C", "C++", "Java", "PHP");
	 
	//printing the index of current element
	echo "Index is: ". key($arr) . "<br>";

	//moving to the next element
	next($arr);
	//now print the index
	echo "Index is: ". key($arr) . "<br>";

	//moving to the next element
	next($arr);
	//now print the index
	echo "Index is: ". key($arr) . "<br>";
?>

Output:

Index is: 0
Index is: 1
Index is: 2

Leave a Comment

error: Alert: Content is protected!!