아래 구문을 사용하여 foreach의 값에 액세스할 수 있습니다.
구문은 다음과 같습니다 -
foreach ($yourArrayName as &$anyVariableName)
다음과 같은 배열이 있다고 가정해 보겠습니다.
$values= array(35, 50, 100, 75);
이제 다음 PHP 코드를 사용하여 각 배열 값을 5로 곱합니다 -
예시
<!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> 출력
175 250 500 375