You are here: Re: [PHP] Assign values in foreach-loop « PHP « IT news, forums, messages
Re: [PHP] Assign values in foreach-loop

Posted by tg-php on 09/07/05 20:28

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.

 

Navigation:

[Reply to this message]


Удаленная работа для программистов  •  Как заработать на Google AdSense  •  England, UK  •  статьи на английском  •  PHP MySQL CMS Apache Oscommerce  •  Online Business Knowledge Base  •  DVD MP3 AVI MP4 players codecs conversion help
Home  •  Search  •  Site Map  •  Set as Homepage  •  Add to Favourites

Copyright © 2005-2006 Powered by Custom PHP Programming

Сайт изготовлен в Студии Валентина Петручека
изготовление и поддержка веб-сайтов, разработка программного обеспечения, поисковая оптимизация