PDA

View Full Version : Updating Array values with foreach??



WearDark
September 7th, 2007, 05:23 PM
In the following loop:




foreach($gig_array as $value){
echo $_POST['gig_info']. ".";
echo $_POST['ticket_info']. ".";
if($value->title == $selected){
echo "VALUE FOUND\n";
$value->blurb = $_POST['gig_info'];
$value->tickets = $_POST['ticket_info'];
}

}
I cannot seem to get the $value to maintain the change once the loop has completed. Maybe im misunderstanding the the foreach loop in itself but doesn't $value represent an array entry and therefore shouldn't changes made to the $value var propogate to the array??

Any ideas?

Cheers.

WearDark
September 7th, 2007, 07:31 PM
Note to self: Check the manual.

"Note: Unless the array is referenced, foreach operates on a copy of the specified array and not the array itself. Therefore, the array pointer is not modified as with the each() construct, and changes to the array element returned are not reflected in the original array. However, the internal pointer of the original array is advanced with the processing of the array. Assuming the foreach loop runs to completion, the array's internal pointer will be at the end of the array."

Esherido
September 8th, 2007, 07:53 AM
^ Seeing wisdom in newbies always makes me happy. :D

If you haven't figured out a workaround, you could probably use a for loop instead which would allow you to change the value because you would have the key. If you'd already figured it out, then congrats and happy coding.