Re: Assign values in foreach-loop
| From: | Jason Davidson | Date: | Wed, 07 Sep 2005 18:31:13 +0000 |
| Subject: | Re: Assign values in foreach-loop | ||
| References: | 1 2 | Groups: | php.general |
| Request: | Send a blank email to php-general+get-221987@lists.php.net to get a copy of this message | ||
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 <gustav@varupiraten.se> wrote:
>
>
> ----- 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/
>
> --
> PHP General Mailing List (http://www.php.net/)
> To unsubscribe, visit: http://www.php.net/unsub.php
>
>