current: to read the value of the array cursor position

We can read the value of the array element using current function.
echo current(array);
Example
$my_array=array("First One", "Second One", "Third One", "Fourth One", "Fifth One" );
echo current($my_array); // Output first one
current reads the value of the present internal pointer or cursor of the array. We can move the internal pointer to next element by using next
Example
$my_array=array("First One", "Second One", "Third One", "Fourth One");
echo current($my_array); // Output first one
next($my_array);
echo current($my_array);// Output Second One

Difference between each and current

By using each we can display the current element of the array but after displaying the internal pointer of the array shifts to next element. In case of current after returning the element the pointer does not move on its own and remains there.

Read the key of the current cursor position
$my_array=array("First One", "Second One", "Third One", "Fourth One", "Fifth One" );
echo key($my_array); // Output is 0 
$my_array=array("First One", "Second One", "Third One", "Fourth One", "Fifth One");

echo current($my_array); // Output : First One 
echo next($my_array); // Output : Second One 
echo next($my_array); // Output : Third One 
echo prev($my_array); // Output : Second One 
echo end($my_array); // Output : Fifth One 
echo reset($my_array); // Output : First One

Example: Iterating Through Arrays with current(), next(), and prev()

$arr = array("apple", "banana", "cherry");
echo current($arr);  // Output: apple
next($arr);
echo current($arr);  // Output: banana
prev($arr);
echo current($arr);  // Output: apple

Example: Using current() with Multidimensional Arrays

$arr = array(
    array("name" => "John", "age" => 30),
    array("name" => "Jane", "age" => 25)
);
echo current($arr)["name"];  // Output: John
next($arr);
echo '<br>';
echo current($arr)["name"];  // Output: Jane
Explanation:
Iterating Arrays: Explains how `current()` can be combined with other pointer functions to move through arrays.
Multidimensional Arrays: Shows how to handle complex arrays with `current()`.
Reset vs Current: Highlights the difference between `current()` and `reset()`.

Array REFERENCE
Subscribe to our YouTube Channel here


Subscribe

* indicates required
Subscribe to plus2net

    plus2net.com







    Post your comments , suggestion , error , requirements etc here





    PHP video Tutorials
    We use cookies to improve your browsing experience. . Learn more
    HTML MySQL PHP JavaScript ASP Photoshop Articles FORUM . Contact us
    ©2000-2024 plus2net.com All rights reserved worldwide Privacy Policy Disclaimer