PHP: each() function
PHP: Return the current key and value pair from an array
The each() function is used to fetch the key and value of the current element. The function also moves the internal array pointer forward.
Note: After each() has executed, the array cursor will be left on the next element of the array, or past the last element if it hits the end of the array. You have to use reset() if you want to traverse the array again using each.
Version
(PHP 4 and above)
Syntax:
each(array_name)
Parameter:
Name | Description | Required / Optional |
Type |
---|---|---|---|
array_name | The input array. | Required | Array |
Return value:
The current key and value pair from array_name.
Value Type: Array
Example:
<?php
$subject= array('Language','English','Math','Science');
$result = each($subject);
print_r($result);
?>
Output:
Array ([1] => Language [value] => Language [0] => 0 [key] => 0)
View the example in the browser
Practice here online :
See also
PHP: Tips of the Day
Filters the collection using the given callback
Example:
<?php function tips_reject($items, $func) { return array_values(array_diff($items, array_filter($items, $func))); } print_r(tips_reject(['Apple', 'Pear', 'Kiwi', 'Banana'], function ($item) { return strlen($item) > 4; })); ?>
Output:
Array ( [0] => Pear [1] => Kiwi )
- Weekly Trends
- Java Basic Programming Exercises
- SQL Subqueries
- Adventureworks Database Exercises
- C# Sharp Basic Exercises
- SQL COUNT() with distinct
- JavaScript String Exercises
- JavaScript HTML Form Validation
- Java Collection Exercises
- SQL COUNT() function
- SQL Inner Join
- JavaScript functions Exercises
- Python Tutorial
- Python Array Exercises
- SQL Cross Join
- C# Sharp Array Exercises