
- PHP 7 Tutorial
- PHP 7 - Home
- PHP 7 - Introduction
- PHP 7 - Performance
- PHP 7 - Environment Setup
- PHP 7 - Scalar Type Declarations
- PHP 7 - Return Type Declarations
- PHP 7 - Null Coalescing Operator
- PHP 7 - Spaceship Operator
- PHP 7 - Constant Arrays
- PHP 7 - Anonymous Classes
- PHP 7 - Closure::call()
- PHP 7 - Filtered unserialize()
- PHP 7 - IntlChar
- PHP 7 - CSPRNG
- PHP 7 - Expectations
- PHP 7 - use Statement
- PHP 7 - Error Handling
- PHP 7 - Integer Division
- PHP 7 - Session Options
- PHP 7 - Deprecated Features
- PHP 7 - Removed Extensions & SAPIs
- PHP 7 Useful Resources
- PHP 7 - Quick Guide
- PHP 7 - Useful Resources
- PHP 7 - Discussion
How to access and return specific value of a foreach in PHP?
You can use the below syntax to access the value of a foreach.
The syntax is as follows −
foreach ($yourArrayName as &$anyVariableName)
Let’s say we have the following array:
$values= array(35, 50, 100, 75);
We will now multiple each array value with 5 using the following PHP code −
Example
<!DOCTYPE html> <html> <body> <?php $values= array(35, 50, 100, 75); function getValues($values) { $allValues=[]; $counter=0; foreach ($values as &$tempValue) { $tempValue = $tempValue * 5; $allValues[$counter]=$tempValue; $counter++; } return $allValues; } $result=getValues($values); for($i=0;$i<count($result);$i++){ echo $result[$i],"<br>"; } ?> </body> </html>
Output
175 250 500 375
- Related Articles
- How to count values from a PHP array and show value only once in a foreach loop?
- MongoDB function to return a specific data/value?
- Difference Between For and Foreach in PHP
- Return the truncated value of a specific array element in Numpy
- PHP foreach Loop.
- Performance of FOR vs FOREACH in PHP
- Stripping last comma from a foreach loop in PHP?
- PHP – How to return character by Unicode code point value using mb_chr()?
- The internal working of the ‘foreach’ loop in PHP
- Multiple index variables in PHP foreach loop
- How to return only value of a field in MongoDB?
- Parsing JSON array with PHP foreach
- How to access the last value in a vector in R?
- Return a specific field in MongoDB?
- How to swap a specific field value in MySQL?

Advertisements