Created
April 29, 2020 21:32
-
-
Save cyberbit/3c086563000650c890ca80ec1c247ca8 to your computer and use it in GitHub Desktop.
Foreach referential variable
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?php | |
$original = [ | |
[ | |
'id' => 1, | |
'name' => 'foo' | |
], | |
[ | |
'id' => 2, | |
'name' => 'bar' | |
] | |
]; | |
$data = $original; | |
print_r($data); | |
echo "<p>"; | |
/** | |
* Array ( [id] => 1 [name] => foo ) | |
* Array ( [id] => 2 [name] => bar ) | |
*/ | |
foreach ($data as $index => $item) { | |
$data[$index]['name'] = $item['name'] . ' something'; | |
print_r($item); | |
echo "<br>"; | |
} | |
$data = $original; | |
echo "<p>"; | |
/** | |
* Array ( [id] => 1 [name] => foo something ) | |
* Array ( [id] => 2 [name] => bar something ) | |
*/ | |
foreach ($data as $index => &$item) { | |
$data[$index]['name'] = $item['name'] . ' something'; | |
print_r($item); | |
echo "<br>"; | |
} | |
?> |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment