PHP: replace array value doesn't stay after foreach loop - php

I'm changing the value in a multi-dimensional array and it's not staying outside of the foreach loop that's being used to traverse it.
My array initially looks something like this:
Array
{
[0] => Array
{
[name] => Bob
[age] => 33
[state] => CA
[visited] => 0
}
...
}
My PHP gets into it by going:
foreach ($people as $person){
echo $person['name']
....
logic for the visited variable
...
$person['visited'] = $calculated_visit_value;
}
If I
print_r($person)
at the end (but inside) of the foreach loop everything looks good, the value for visited is set. However, if I print_r($people) outside of the loop, $person['visited'] is not set. I don't know what I'm doing wrong.
Help is appreciated.

You are creating a new variable called $person from within that for loop and your array will never see the scope of that new variable.
You can try passing it by reference, like so:
foreach ($people as &$person){
echo $person['name'];
....
logic for the visited variable
...
$person['visited'] = $calculated_visit_value;
}

From foreach's documentation:
Unless the array is referenced, foreach operates on a copy of the
specified array and not the array itself. foreach has some side
effects on the array pointer. Don't rely on the array pointer during
or after the foreach without resetting it.
What this means is that your $person variable is a copy of what was in the array, similar in effect to this code (note that this code is for understanding only and wrong on many levels, in reality you would use the reset(), current() and next() function to loop properly over your array, see here):
for ($i = 0; $i < count($people); $i++) {
$person = $people[$i];
// code inside your foreach ...
}
So if you change the content of $person, you don't actually modify what's inside the $people array
To solve that, you can either use a referenced foreach:
foreach ($people as &$person) { // note the &
$person = $calculated_visit_value; // $person is now a reference to the original value inside $people and thus this will work as intended
}
Note that the refence is not cleared when the foreach loop ends, so at the end of it $person is still a reference to the last element of $people.
If you don't know what references are, please refer to the documentation for more info.
Or use the key to access the original array:
foreach ($people as $person_index => $person) {
$people[$person_index] = $calculated_visit_value;
}
For your information, you can use the two together
foreach ($people as $person_index => &$person { ...

The $person array is generated on each iteration, so setting that value would be overwritten on the next go through anyway.
But even so, that array only exists during the loop. You should create another array before the loop and put your values into that array during the loop.

As it has been told, "you are creating a new variable called $person from within that for loop and your array will never see the scope of that new variable."
I find this solution more robust :
foreach ($people as $key => $person)
{
echo $person['name'];
//logic for the visited variable
$people[$key]['visited']=$calculated_visit_value;
}

Related

How to avoid to duplicate foreach if the logic is the same for all array?

This question is about how to produce a better code in PHP.
I've multiple arrays (5 to be exact). I've to apply the same logic to all of them.
How can I avoid to duplicate the code for all of them ?
$cpus = getCPUs(); // Get array 1 dimension with key
$rams = getRAMs(); // Get array 1 dimension with key
I mean, the code inside, I just have to create a function. That's OK.
But I still have to declare one foreach loop for each array...
Is there a way to avoid that ? It's like to have my foreach loop parameters from variables.
foreach ($cpus as $key_cpu => &$cpu) {
// FUNCTION XXX
}
foreach ($rams as $key_ram => &$ram) {
// FUNCTION XXX
}
Regards
You could simply use two foreachs:
foreach ([&$cpus, &$rams] as &$components) {
foreach ($components as $key => &$component) {
// FUNCTION XXX
}
}
Note that all those references are needed to be able to assign another value to $component and have it also modify the original values (like the question suggested). Ideally you'd want to avoid doing that if you can. If these components are arrays, explore using objects instead.
Thanks to #AterLux for the helpful comments below.
write common function/method for loop all array and return whatever your want...
function loop_array($array){
$return_data=array()
foreach ($array as $key => &$data) {
// FUNCTION XXX
}
return $return_data;
}

Manipulate an array when looping

So $tr['tree'] is an array. $dic is an array stored as key values. I want to add the key source to that those arrays. It looks like the following code doesn't work as expected as I'm guessing $dic is a new instance of the array object inside $tr['tree'].
foreach($tr['tree'] as $dic){
$dic['source'] = $tr['source']." > ".$dic['name'];
}
Note, I'm coming from python where this would work brilliantly. So how would I do this in PHP?
foreach() creates copies of the items you're looping on, so $dic in the loop is detached from the array. If you want to modify the parent array, the safe method is to use:
foreach($array as $key => $value) {
$array[$key] = $new_value;
}
You could use a reference:
foreach($array as &$value) {
^---
$value = $new_value;
}
but that can lead to stupidly-hard-to-find bugs later. $value will REMAIN a reference after the foreach terminates. If you re-use that variable name later on for other stuff, you'll be modifying the array, because the var still points at it.

Unexpected behavior with references

I have some unexpected behavior with references:
foreach ($this->data as &$row)
{
$row['h'] = 1;
}
foreach ($this->data as $id => $row)
{
... in some cases $row[$id] = $row;
}
The result is that the last element of the array is replaced with the second to last element of the array. It is fixed with the following code:
foreach ($this->data as $key => $row)
{
$this->data[$key]['h'] = 1;
}
Unfortunately, I don't have more time to spend on this. Maybe it is an error with PHP (PHP 5.5.9-1ubuntu4) or something I don't know about references?
There is a perfectly logical explanation and this is not a bug!
PHP 5 introduces the possibility of modifying the contents of the array directly by assigning the value of each element to the iterated variable by reference rather than by value. Consider this code, for example:
$a = array (’zero’,’one’,’two’);
foreach ($a as &$v) {
}
foreach ($a as $v) {
}
print_r ($a);
It would be natural to think that, since this little script does nothing to the array, it will not affect its contents... but that’s not the case! In fact, the script provides the following output:
Array
(
[0] => zero
[1] => one
[2] => one
)
As you can see, the array has been changed, and the last key now contains the value ’one’. How is that possible? The first foreach loop does not make any change to the array, just as we would expect. However, it does cause $v to be assigned a reference to each of $a’s elements, so that, by the time the loop is over, $v is, in fact, a reference to $a[2].
As soon as the second loop starts, $v is now assigned the value of each element. However, $v is already a reference to $a[2]; therefore, any value assigned to it will be copied automatically into the last element of the arrays! Thus, during the first iteration, $a[2] will become zero, then one, and then one again, being effectively copied on to itself. To solve this problem, you should always unset the variables you use in your by-reference foreach loops—or, better yet, avoid using the former altogether.
When looping over an array by reference, you need to manually let go of the reference at the end of your for loop to avoid weird behaviors like this one. So your first foreach should be:
foreach ($this->data as &$row)
{
.... code ....
}
unset($row);
In this case, unset is only destroying the reference, not the contents referenced by $row.
See the warning in the PHP foreach documentation

Strange foreach loop after modifying array

As I wrote some code, PHP confused me a little as I didn't expected the result of the following code:
$data = array(array('test' => 'one'), array('test' => 'two'));
foreach($data as &$entry) {
$entry['test'] .= '+';
}
foreach($data as $entry) {
echo $entry['test']."\n";
}
I think it should output
one+
two+
However the result is: http://ideone.com/e5tCsi
one+
one+
Can anyone explain to me why?
This is expected behaviour, see also https://bugs.php.net/bug.php?id=29992.
The reference is maintained when using the second foreach, so when using the second foreach the value of $entry, which points still to $data[1], is overwritten with the first value.
P.s. (thanks to #billyonecan for saying it): you need to unset($entry) first, so that your reference is destroyed.
This is mentioned specifically in the documentation for foreach. You should unset the loop variable when it gets elements of the array by reference.
Warning
Reference of a $value and the last array element remain even after the
foreach loop. It is recommended to destroy it by unset().

Using foreach effectively in PHP

So I don't think I'm making full use of the foreach loop. Here is how I understand foreach.
It goes like foreach(arrayyouwanttoloopthrough as onevalueofthatarray)
No counter or incrementing required, it automatically pulls an array, value by value each loop, and for that loop it calls the value whatever is after the "as".
Stops once it's done with the array.
Should basically replace "for", as long as dealing with an array.
So something I try to do a lot with foreach is modify the array values in the looping array. But in the end I keep finding I have to use a for loop for that type of thing.
So lets say that I have an array (thing1, thing2, thing3, thing4) and I wanted to change it....lets say to all "BLAH", with a number at the end, I'd do
$counter = 0;
foreach($array as $changeval){
$counter++;
$changeval = "BLAH".$counter;
}
I would think that would change it because $changeval should be whatever value it's at for the array, right? But it doesn't. The only way I could find to do that in a] foreach is to set a counter (like above), and use the array with the index of counter. But to do that I'd have to set the counter outside the loop, and it's not even always reliable. For that I'd think it would be better to use a for loop instead of foreach.
So why would you use foreach over for? I think I'm missing something here because foreach has GOT to be able to change values...
Thanks
OH HEY One more thing. Are variables set in loops (like i, or key) accessible outside the loop? If I have 2 foreach loops like
foreach(thing as value)
would I have to make the second one
foreach(thing2 as value2) ]
or else it would have some problems?
You can use a reference variable instead:
foreach ($array as &$value)
{
$value = "foo";
}
Now the array is full of foo (note the & before $value).
Normally, the loop variable simply contains a copy of the corresponding array element, but the & (ampersand) tells PHP to make it a reference to the actual array element, rather than just a copy; hence you can modify it.
However, as #Tadeck says below, you should be careful in this case to destroy the reference after the loop has finished, since $value will still point to the final element in the array (so it's possible to accidentally modify it). Do this with unset:
unset($value);
The other option would be to use the $key => $value syntax:
foreach ($array as $key => $value)
{
$array[$key] = "foo";
}
To answer your second question: yes, they are subsequently accessible outside the loop, which is why it's good practice to use unset when using reference loop variables as in my first example. However, there's no need to use new variable names in subsequent loops, since the old ones will just be overwritten (with no unwanted consequences).
You want to pass by reference:
$arr = array(1, 2, 3, 4);
foreach ($arr as &$value)
{
$value = $value * 2;
}
// $arr is now array(2, 4, 6, 8)
The extended foreach syntax is like this:
foreach ($array as $key => $value) {
}
Using the $key, you can index the original array:
foreach ($array as $key => $value) {
$array[$key] = "BLAH";
}
Incrementing from 0 to ...
foreach($array as $changeval){
if (!isset($counter)) { $counter = 0; }
$counter++;
$changeval = "BLAH".$counter;
}
Using the index/key of the ARRAY
foreach($array as $key => $changeval){
$changeval = "BLAH".$key;
}
You can use the key when looping with foreach:
foreach ($array as $key => $value)
{
$array[$key] = "foo";
}
But note that using a reference like Will suggested will be faster for such a case - but anyway, the $key => $value-syntax is quite useful sometimes.

Categories