Re: Assign values in foreach-loop

[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

 



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@xxxxxxxxxxxxxx> wrote:
> 
> 
> ----- Original Message -----
> From: "Sabine" <phpdev@xxxxxxx>
> To: "PHP general" <php-general@xxxxxxxxxxxxx>
> Sent: Wednesday, September 07, 2005 7:14 PM
> Subject:  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
> 
>

[Index of Archives]     [PHP Home]     [Apache Users]     [PHP on Windows]     [Kernel Newbies]     [PHP Install]     [PHP Classes]     [Pear]     [Postgresql]     [Postgresql PHP]     [PHP on Windows]     [PHP Database Programming]     [PHP SOAP]

  Powered by Linux