Re: Assign values in foreach-loop

From: Date: Wed, 07 Sep 2005 18:23:03 +0000
Subject: Re: Assign values in foreach-loop
References: 1  Groups: php.general 
Request: Send a blank email to php-general+get-221986@lists.php.net to get a copy of this message
----- Original Message ----- From: "Sabine" <phpdev@sric.de> To: "PHP general" <php-general@lists.php.net> 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/

« previous php.general (#221986) next »