1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
| $myArray = array(1, 2, 3);
// Operation on array copy (not-in-place)
foreach ($myArray as $key => $value) {
$value = $value * 2;
}
// Operation on same array (in-place)
foreach ($myArray as $key => $value) {
$myArray[$key] = $value * 2;
}
// Operation on same array (in-place, shortform)
foreach ($myArray as &$value) {
$value = $value * 2;
}
// Nicely display array values
echo '<pre>';
var_dump($myArray);
echo '< /pre>'; |
$myArray = array(1, 2, 3);
// Operation on array copy (not-in-place)
foreach ($myArray as $key => $value) {
$value = $value * 2;
}
// Operation on same array (in-place)
foreach ($myArray as $key => $value) {
$myArray[$key] = $value * 2;
}
// Operation on same array (in-place, shortform)
foreach ($myArray as &$value) {
$value = $value * 2;
}
// Nicely display array values
echo '<pre>';
var_dump($myArray);
echo '< /pre>';