Re: [PHP] Assign values in foreach-loop
Thanks, Gustav for your answer. I already solved my problem (see below) with help of other members of the list. Gustav Wiberg schrieb: - Original Message - From: "Sabine" <[EMAIL PROTECTED]> To: "PHP general" Sent: Wednesday, September 07, 2005 7:14 PM Subject: [PHP] Assign values in foreach-loop I might add that is not always a good thing to use true or false with sessions. Use 1 and 0 instead. (or two diffrent numbers or anything else but true or false) Why? What is wrong with using true or false with sessions? What may happen? Sabine /G http://www.varupiraten.se/ -- PHP General Mailing List (http://www.php.net/) To unsubscribe, visit: http://www.php.net/unsub.php
Re: [PHP] Assign values in foreach-loop
In case it hasnt been said already, as long as your array has numeric and consecutive keys, you could use 'for' instead of 'foreach'. Jason On 9/7/05, Gustav Wiberg <[EMAIL PROTECTED]> wrote: > > > - Original Message - > From: "Sabine" <[EMAIL PROTECTED]> > To: "PHP general" > Sent: Wednesday, September 07, 2005 7:14 PM > Subject: [PHP] Assign values in foreach-loop > > > > Hello to all, > > > > is it possible to assign values to the array for which I do the > > foreach-loop? > > > > foreach ($_SESSION['arr1'] as $arr1) { > > foreach ($_SESSION['arr2'] as $arr2) { > > if ($arr1['id'] == $arr2['id']) { > > $arr1['selected'] = true; > > } > > } } > > > > I think $arr1 is only a temp-var, so the assignment won't reflect on > > $_SESSION['arr1'] . Is that right? > > Surely I can do it with a for-loop, but those arrays are a bit complex > and > > a foreach would be much easier to read. > > > > Thanks in advance for your answers > > Sabine > > > > -- > > PHP General Mailing List (http://www.php.net/) > > To unsubscribe, visit: http://www.php.net/unsub.php > > > > > > > > -- > > No virus found in this incoming message. > > Checked by AVG Anti-Virus. > > Version: 7.0.344 / Virus Database: 267.10.18/91 - Release Date: > 2005-09-06 > > > > > Hi there! > > Why not set $_SESSION['arr1'] = true ? > > > foreach ($_SESSION['arr1'] as $arr1) { > > foreach ($_SESSION['arr2'] as $arr2) { > > if ($arr1['id'] == $arr2['id']) { > > $_SESSION['arr1'] = true; > > } > > } } > > > > I think $arr1 is only a temp-var, so the assignment won't reflect on > > $_SESSION['arr1'] . Is that right? > > I might add that is not always a good thing to use true or false with > sessions. Use 1 and 0 instead. (or two diffrent numbers or anything else > but > true or false) > > /G > http://www.varupiraten.se/ > > -- > PHP General Mailing List (http://www.php.net/) > To unsubscribe, visit: http://www.php.net/unsub.php > >
Re: [PHP] Assign values in foreach-loop
- Original Message - From: "Sabine" <[EMAIL PROTECTED]> To: "PHP general" Sent: Wednesday, September 07, 2005 7:14 PM Subject: [PHP] Assign values in foreach-loop Hello to all, is it possible to assign values to the array for which I do the foreach-loop? foreach ($_SESSION['arr1'] as $arr1) { foreach ($_SESSION['arr2'] as $arr2) { if ($arr1['id'] == $arr2['id']) { $arr1['selected'] = true; } } } I think $arr1 is only a temp-var, so the assignment won't reflect on $_SESSION['arr1'] . Is that right? Surely I can do it with a for-loop, but those arrays are a bit complex and a foreach would be much easier to read. Thanks in advance for your answers Sabine -- PHP General Mailing List (http://www.php.net/) To unsubscribe, visit: http://www.php.net/unsub.php -- No virus found in this incoming message. Checked by AVG Anti-Virus. Version: 7.0.344 / Virus Database: 267.10.18/91 - Release Date: 2005-09-06 Hi there! Why not set $_SESSION['arr1'] = true ? foreach ($_SESSION['arr1'] as $arr1) { foreach ($_SESSION['arr2'] as $arr2) { if ($arr1['id'] == $arr2['id']) { $_SESSION['arr1'] = true; } } } I think $arr1 is only a temp-var, so the assignment won't reflect on $_SESSION['arr1'] . Is that right? I might add that is not always a good thing to use true or false with sessions. Use 1 and 0 instead. (or two diffrent numbers or anything else but true or false) /G http://www.varupiraten.se/ -- PHP General Mailing List (http://www.php.net/) To unsubscribe, visit: http://www.php.net/unsub.php
Re: [PHP] Assign values in foreach-loop
Jordan Miller wrote: Hello, You simply need the $key variable, then you can set a new value for $arr[$key] for each array element: $value) { $arr[$key] = $value * 2; } // $arr is now array(2, 4, 6, 8) ?> http://www.php.net/foreach If you have PHP 5, you can perhaps more efficiently do this: As of PHP 5, you can easily modify array's elements by preceding $value with &. This will assign reference instead of copying the value. Jordan Or you can do it like that... Totally brain-faded on the $key bit. I've been bitten by this so many times that it's just in my head that you can't permanently modify the array. Cool stuff on the v5 referencing, I guess I'm gonna have to go back through the manual on everything and not just the new stuff. Thanks! -- PHP General Mailing List (http://www.php.net/) To unsubscribe, visit: http://www.php.net/unsub.php
[PHP] Assign values in foreach-loop: Problem solved
Thanks a lot for your fast answers, I'll do it like -TG and Jordan in his last post suggested! Sabine Original-Nachricht Betreff: [PHP] Assign values in foreach-loop Datum: Wed, 07 Sep 2005 19:14:40 +0200 Von:Sabine <[EMAIL PROTECTED]> Antwort an: [EMAIL PROTECTED] An: PHP general Hello to all, is it possible to assign values to the array for which I do the foreach-loop? foreach ($_SESSION['arr1'] as $arr1) { foreach ($_SESSION['arr2'] as $arr2) { if ($arr1['id'] == $arr2['id']) { $arr1['selected'] = true; } } } I think $arr1 is only a temp-var, so the assignment won't reflect on $_SESSION['arr1'] . Is that right? Surely I can do it with a for-loop, but those arrays are a bit complex and a foreach would be much easier to read. Thanks in advance for your answers Sabine -- PHP General Mailing List (http://www.php.net/) To unsubscribe, visit: http://www.php.net/unsub.php -- PHP General Mailing List (http://www.php.net/) To unsubscribe, visit: http://www.php.net/unsub.php
Re: [PHP] Assign values in foreach-loop
sorry, i didn't fully answer the questions... if i understand your multidimensional array correctly, your code should be something like: foreach ($_SESSION['arr1'] as $key => $arr1) { foreach ($_SESSION['arr2'] as $arr2) { if ($arr1['id'] == $arr2['id']) { $_SESSION['arr1'][$key]['selected'] = true; } } } Jordan On Sep 7, 2005, at 12:22 PM, Jordan Miller wrote: foreach ($_SESSION['arr1'] as $arr1) { foreach ($_SESSION['arr2'] as $arr2) { if ($arr1['id'] == $arr2['id']) { $arr1['selected'] = true; } } } -- PHP General Mailing List (http://www.php.net/) To unsubscribe, visit: http://www.php.net/unsub.php
Re: [PHP] Assign values in foreach-loop
Yeah, you can assign values, but you have to keep track of keys, so you'll want to use the => operator for foreach (pardon my reformatted, I feel naked without my braces): foreach ($_SESSION['arr1'] as $key1 => $arr1) { foreach ($_SESSION['arr2'] as $key2 => $arr2) { if ($arr1['id'] == $arr2['id']) { # $arr1['selected'] = true; $_SESSION['arr1'][$key1]['selected'] = true; } } } Is that what you were trying to do? -TG = = = Original message = = = Hello to all, is it possible to assign values to the array for which I do the foreach-loop? foreach ($_SESSION['arr1'] as $arr1) foreach ($_SESSION['arr2'] as $arr2) if ($arr1['id'] == $arr2['id']) $arr1['selected'] = true; I think $arr1 is only a temp-var, so the assignment won't reflect on $_SESSION['arr1'] . Is that right? Surely I can do it with a for-loop, but those arrays are a bit complex and a foreach would be much easier to read. Thanks in advance for your answers Sabine -- PHP General Mailing List (http://www.php.net/) To unsubscribe, visit: http://www.php.net/unsub.php ___ Sent by ePrompter, the premier email notification software. Free download at http://www.ePrompter.com. -- PHP General Mailing List (http://www.php.net/) To unsubscribe, visit: http://www.php.net/unsub.php
Re: [PHP] Assign values in foreach-loop
Hello, You simply need the $key variable, then you can set a new value for $arr[$key] for each array element: $value) { $arr[$key] = $value * 2; } // $arr is now array(2, 4, 6, 8) ?> http://www.php.net/foreach If you have PHP 5, you can perhaps more efficiently do this: As of PHP 5, you can easily modify array's elements by preceding $value with &. This will assign reference instead of copying the value. Jordan On Sep 7, 2005, at 12:14 PM, Sabine wrote: Hello to all, is it possible to assign values to the array for which I do the foreach-loop? foreach ($_SESSION['arr1'] as $arr1) { foreach ($_SESSION['arr2'] as $arr2) { if ($arr1['id'] == $arr2['id']) { $arr1['selected'] = true; } } } I think $arr1 is only a temp-var, so the assignment won't reflect on $_SESSION['arr1'] . Is that right? Surely I can do it with a for-loop, but those arrays are a bit complex and a foreach would be much easier to read. Thanks in advance for your answers Sabine -- PHP General Mailing List (http://www.php.net/) To unsubscribe, visit: http://www.php.net/unsub.php -- PHP General Mailing List (http://www.php.net/) To unsubscribe, visit: http://www.php.net/unsub.php
Re: [PHP] Assign values in foreach-loop
Sabine wrote: Hello to all, is it possible to assign values to the array for which I do the foreach-loop? foreach ($_SESSION['arr1'] as $arr1) { foreach ($_SESSION['arr2'] as $arr2) { if ($arr1['id'] == $arr2['id']) { $arr1['selected'] = true; } } } I think $arr1 is only a temp-var, so the assignment won't reflect on $_SESSION['arr1'] . Is that right? Surely I can do it with a for-loop, but those arrays are a bit complex and a foreach would be much easier to read. Thanks in advance for your answers Sabine Short answer is no. foreach works on a copy of the array, so any chnges you make inside the foreach loop are "lost" once you're outside of it. Although *inside* the foreach the changes *are* valid. What you can do is: $newArray = array(); foreach ($_SESSION['arr1'] as $arr1) { foreach ($_SESSION['arr2'] as $arr2) { if ($arr1['id'] == $arr2['id']) { $newArray['selected'] = true; } } } Then reference $newArray instead of $arr1. -- PHP General Mailing List (http://www.php.net/) To unsubscribe, visit: http://www.php.net/unsub.php
[PHP] Assign values in foreach-loop
Hello to all, is it possible to assign values to the array for which I do the foreach-loop? foreach ($_SESSION['arr1'] as $arr1) { foreach ($_SESSION['arr2'] as $arr2) { if ($arr1['id'] == $arr2['id']) { $arr1['selected'] = true; } } } I think $arr1 is only a temp-var, so the assignment won't reflect on $_SESSION['arr1'] . Is that right? Surely I can do it with a for-loop, but those arrays are a bit complex and a foreach would be much easier to read. Thanks in advance for your answers Sabine -- PHP General Mailing List (http://www.php.net/) To unsubscribe, visit: http://www.php.net/unsub.php