Skip to content

Instantly share code, notes, and snippets.

@cyberbit
Created April 29, 2020 21:32
Show Gist options
  • Save cyberbit/3c086563000650c890ca80ec1c247ca8 to your computer and use it in GitHub Desktop.
Save cyberbit/3c086563000650c890ca80ec1c247ca8 to your computer and use it in GitHub Desktop.
Foreach referential variable
<?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