Re: [PHP] modifying within foreach
AddBrainHandler x-httpd-php5 .php On Tue, Jun 23, 2009 at 4:25 PM, Robert Cummings wrote: > Ashley Sheridan wrote: > >> On Tue, 2009-06-23 at 15:07 -0400, Eddie Drapkin wrote: >> >>> It's just foreach($foo as $key => &$item) { } >>> >>> You can't assign the key by reference >.> >>> >>> On Tue, Jun 23, 2009 at 3:04 PM, Ashley >>> Sheridan wrote: >>> On Tue, 2009-06-23 at 12:56 -0600, kirk.john...@zootweb.com wrote: > Andres Gonzalez wrote on 06/23/2009 12:26:38 > PM: > > I want to modify $results within the foreach. In other words, >> during a given pass of this iteration, I want to delete some >> of the items based on particular conditions. Then on the next >> pass thru the foreach, I want $results to be the newer, modified >> array. >> >> This does not seem to work. It appears that the foreach statement >> is implemented such that $results is read into memory at the start >> so that any modifications I make to it during a given pass, are >> ignored >> on the next pass. Is this true? >> > foreach works on a copy of an array, so the behavior you saw is > expected. > See the online manual. > > You could use a while loop, or, instead of unset-ing elements of > $results, > store the elements you want to keep into a new array. > > Kirk > What about passing it by reference? foreach($results as &$key => &$item) { // modify items here } Thanks Ash www.ashleysheridan.co.uk -- PHP General Mailing List (http://www.php.net/) To unsubscribe, visit: http://www.php.net/unsub.php >> Yeah, hehe, I was trying to remember off the top of my head, and >> obviously forgot! :p >> >> *slaps self* >> > > Your brain is in PHP4 mode... > > *slaps Ashley* > > >:) > > Cheers, > Rob. > -- > http://www.interjinn.com > Application and Templating Framework for PHP > > -- > PHP General Mailing List (http://www.php.net/) > To unsubscribe, visit: http://www.php.net/unsub.php > > -- Martin Scotta
Re: [PHP] modifying within foreach
It's just foreach($foo as $key => &$item) { } You can't assign the key by reference >.> On Tue, Jun 23, 2009 at 3:04 PM, Ashley Sheridan wrote: > On Tue, 2009-06-23 at 12:56 -0600, kirk.john...@zootweb.com wrote: >> Andres Gonzalez wrote on 06/23/2009 12:26:38 PM: >> >> > I want to modify $results within the foreach. In other words, >> > during a given pass of this iteration, I want to delete some >> > of the items based on particular conditions. Then on the next >> > pass thru the foreach, I want $results to be the newer, modified >> > array. >> > >> > This does not seem to work. It appears that the foreach statement >> > is implemented such that $results is read into memory at the start >> > so that any modifications I make to it during a given pass, are ignored >> > on the next pass. Is this true? >> >> foreach works on a copy of an array, so the behavior you saw is expected. >> See the online manual. >> >> You could use a while loop, or, instead of unset-ing elements of $results, >> store the elements you want to keep into a new array. >> >> Kirk > > What about passing it by reference? > > foreach($results as &$key => &$item) > { > // modify items here > } > > Thanks > Ash > www.ashleysheridan.co.uk > > > -- > 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] modifying within foreach
Ashley Sheridan wrote: On Tue, 2009-06-23 at 15:07 -0400, Eddie Drapkin wrote: It's just foreach($foo as $key => &$item) { } You can't assign the key by reference >.> On Tue, Jun 23, 2009 at 3:04 PM, Ashley Sheridan wrote: On Tue, 2009-06-23 at 12:56 -0600, kirk.john...@zootweb.com wrote: Andres Gonzalez wrote on 06/23/2009 12:26:38 PM: I want to modify $results within the foreach. In other words, during a given pass of this iteration, I want to delete some of the items based on particular conditions. Then on the next pass thru the foreach, I want $results to be the newer, modified array. This does not seem to work. It appears that the foreach statement is implemented such that $results is read into memory at the start so that any modifications I make to it during a given pass, are ignored on the next pass. Is this true? foreach works on a copy of an array, so the behavior you saw is expected. See the online manual. You could use a while loop, or, instead of unset-ing elements of $results, store the elements you want to keep into a new array. Kirk What about passing it by reference? foreach($results as &$key => &$item) { // modify items here } Thanks Ash www.ashleysheridan.co.uk -- PHP General Mailing List (http://www.php.net/) To unsubscribe, visit: http://www.php.net/unsub.php Yeah, hehe, I was trying to remember off the top of my head, and obviously forgot! :p *slaps self* Your brain is in PHP4 mode... *slaps Ashley* >:) Cheers, Rob. -- http://www.interjinn.com Application and Templating Framework for PHP -- PHP General Mailing List (http://www.php.net/) To unsubscribe, visit: http://www.php.net/unsub.php
Re: [PHP] modifying within foreach
On Tue, 2009-06-23 at 15:07 -0400, Eddie Drapkin wrote: > It's just foreach($foo as $key => &$item) { } > > You can't assign the key by reference >.> > > On Tue, Jun 23, 2009 at 3:04 PM, Ashley > Sheridan wrote: > > On Tue, 2009-06-23 at 12:56 -0600, kirk.john...@zootweb.com wrote: > >> Andres Gonzalez wrote on 06/23/2009 12:26:38 PM: > >> > >> > I want to modify $results within the foreach. In other words, > >> > during a given pass of this iteration, I want to delete some > >> > of the items based on particular conditions. Then on the next > >> > pass thru the foreach, I want $results to be the newer, modified > >> > array. > >> > > >> > This does not seem to work. It appears that the foreach statement > >> > is implemented such that $results is read into memory at the start > >> > so that any modifications I make to it during a given pass, are ignored > >> > on the next pass. Is this true? > >> > >> foreach works on a copy of an array, so the behavior you saw is expected. > >> See the online manual. > >> > >> You could use a while loop, or, instead of unset-ing elements of $results, > >> store the elements you want to keep into a new array. > >> > >> Kirk > > > > What about passing it by reference? > > > > foreach($results as &$key => &$item) > > { > >// modify items here > > } > > > > Thanks > > Ash > > www.ashleysheridan.co.uk > > > > > > -- > > PHP General Mailing List (http://www.php.net/) > > To unsubscribe, visit: http://www.php.net/unsub.php > > > > Yeah, hehe, I was trying to remember off the top of my head, and obviously forgot! :p *slaps self* Thanks Ash www.ashleysheridan.co.uk -- PHP General Mailing List (http://www.php.net/) To unsubscribe, visit: http://www.php.net/unsub.php
Re: [PHP] modifying within foreach
Thanks guys--passing by reference solved my problem. I was not aware that the foreach statement works on a copy. This was explained in the online documentation. Duh!! Thanks again, -Andres Ashley Sheridan wrote: On Tue, 2009-06-23 at 12:56 -0600, kirk.john...@zootweb.com wrote: Andres Gonzalez wrote on 06/23/2009 12:26:38 PM: I want to modify $results within the foreach. In other words, during a given pass of this iteration, I want to delete some of the items based on particular conditions. Then on the next pass thru the foreach, I want $results to be the newer, modified array. This does not seem to work. It appears that the foreach statement is implemented such that $results is read into memory at the start so that any modifications I make to it during a given pass, are ignored on the next pass. Is this true? foreach works on a copy of an array, so the behavior you saw is expected. See the online manual. You could use a while loop, or, instead of unset-ing elements of $results, store the elements you want to keep into a new array. Kirk What about passing it by reference? foreach($results as &$key => &$item) { // modify items here } Thanks Ash www.ashleysheridan.co.uk -- PHP General Mailing List (http://www.php.net/) To unsubscribe, visit: http://www.php.net/unsub.php
RE: [PHP] modifying within foreach
Dude. Use your common sense and the example I just provided: foreach ($results as $key => $item) { if ($item == 'foo') unset($results[$key]); else ($item == 'bar') $results[$key] = 'new value'; } so using your example: foreach ($results as $key => $item) { if ($item[0] == 'value1') unset($results[$key][0]); else ($item[0] == 'bar') $results[$key][0] = 'value4'; } But since you have a multidimensional array, you have to really do two loops to be useful: foreach ($results as $key => $item) { foreach ($item as $i => $value) { if ($value == 'value1') unset($results[$key][$i]) else ($value == 'bar') $results[$key][$i] = 'value4'; } } Or something to that effect, I just wrote that freehand, but you should get the idea. http://daevid.com > -Original Message- > From: Andres Gonzalez [mailto:and...@packetstorm.com] > Sent: Tuesday, June 23, 2009 11:45 AM > Cc: php-general@lists.php.net > Subject: Re: [PHP] modifying within foreach > > I do not want to delete the whole array, only a particular $item. > given this $results array: > > Array > ( > ["key1"] => Array > ( > [0] => value1 > [1] => value2 > [2] => value3 > ( > ["key2"] => Array > ( > [0] => value4 > [1] => value5 > [2] => value6 > ) > ) > > It is a value item that I want to delete based on a > particular criteria. > In each pass I may delete a value item. However, it seems that each > subsequent pass operates on the original $results array and not > the modified one. > > -Andres > > > > Daevid Vincent wrote: > > foreach ($results as $key => $item) > > { > > if ($item == 'foo') unset($results[$key]); > > } > > > > > >> -Original Message- > >> From: Andres Gonzalez [mailto:and...@packetstorm.com] > >> Sent: Tuesday, June 23, 2009 11:27 AM > >> To: php-general@lists.php.net > >> Subject: [PHP] modifying within foreach > >> > >> In the following example: > >> > >> foreach ($results as $key => $item) { > >> > >> //bla bla bla -- unset some of the $items > >> > >> } > >> > >> I want to modify $results within the foreach. In other words, > >> during a given pass of this iteration, I want to delete some > >> of the items based on particular conditions. Then on the next > >> pass thru the foreach, I want $results to be the newer, modified > >> array. > >> > >> This does not seem to work. It appears that the foreach statement > >> is implemented such that $results is read into memory at the start > >> so that any modifications I make to it during a given pass, > >> are ignored > >> on the next pass. Is this true? > >> > >> If so, is there a way that I can tell the foreach statement > >> to re-read the > >> array $results? Or am I just going against the grain here? > >> > >> -Andres > >> > >> > >> -- > >> 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 > -- PHP General Mailing List (http://www.php.net/) To unsubscribe, visit: http://www.php.net/unsub.php
Re: [PHP] modifying within foreach
On Tue, 2009-06-23 at 12:56 -0600, kirk.john...@zootweb.com wrote: > Andres Gonzalez wrote on 06/23/2009 12:26:38 PM: > > > I want to modify $results within the foreach. In other words, > > during a given pass of this iteration, I want to delete some > > of the items based on particular conditions. Then on the next > > pass thru the foreach, I want $results to be the newer, modified > > array. > > > > This does not seem to work. It appears that the foreach statement > > is implemented such that $results is read into memory at the start > > so that any modifications I make to it during a given pass, are ignored > > on the next pass. Is this true? > > foreach works on a copy of an array, so the behavior you saw is expected. > See the online manual. > > You could use a while loop, or, instead of unset-ing elements of $results, > store the elements you want to keep into a new array. > > Kirk What about passing it by reference? foreach($results as &$key => &$item) { // modify items here } Thanks Ash www.ashleysheridan.co.uk -- PHP General Mailing List (http://www.php.net/) To unsubscribe, visit: http://www.php.net/unsub.php
Re: [PHP] modifying within foreach
Andres Gonzalez wrote on 06/23/2009 12:26:38 PM: > I want to modify $results within the foreach. In other words, > during a given pass of this iteration, I want to delete some > of the items based on particular conditions. Then on the next > pass thru the foreach, I want $results to be the newer, modified > array. > > This does not seem to work. It appears that the foreach statement > is implemented such that $results is read into memory at the start > so that any modifications I make to it during a given pass, are ignored > on the next pass. Is this true? foreach works on a copy of an array, so the behavior you saw is expected. See the online manual. You could use a while loop, or, instead of unset-ing elements of $results, store the elements you want to keep into a new array. Kirk
Re: [PHP] modifying within foreach
I do not want to delete the whole array, only a particular $item. given this $results array: Array ( ["key1"] => Array ( [0] => value1 [1] => value2 [2] => value 3 ( ["key2"] => Array ( [0] => value4 [1] => value5 [2] => value6 ) ) It is a value item that I want to delete based on a particular criteria. In each pass I may delete a value item. However, it seems that each subsequent pass operates on the original $results array and not the modified one. -Andres Daevid Vincent wrote: foreach ($results as $key => $item) { if ($item == 'foo') unset($results[$key]); } -Original Message- From: Andres Gonzalez [mailto:and...@packetstorm.com] Sent: Tuesday, June 23, 2009 11:27 AM To: php-general@lists.php.net Subject: [PHP] modifying within foreach In the following example: foreach ($results as $key => $item) { //bla bla bla -- unset some of the $items } I want to modify $results within the foreach. In other words, during a given pass of this iteration, I want to delete some of the items based on particular conditions. Then on the next pass thru the foreach, I want $results to be the newer, modified array. This does not seem to work. It appears that the foreach statement is implemented such that $results is read into memory at the start so that any modifications I make to it during a given pass, are ignored on the next pass. Is this true? If so, is there a way that I can tell the foreach statement to re-read the array $results? Or am I just going against the grain here? -Andres -- 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] modifying within foreach
foreach ($results as $key => $item) { if ($item == 'foo') unset($results[$key]); } > -Original Message- > From: Andres Gonzalez [mailto:and...@packetstorm.com] > Sent: Tuesday, June 23, 2009 11:27 AM > To: php-general@lists.php.net > Subject: [PHP] modifying within foreach > > In the following example: > > foreach ($results as $key => $item) { > > //bla bla bla -- unset some of the $items > > } > > I want to modify $results within the foreach. In other words, > during a given pass of this iteration, I want to delete some > of the items based on particular conditions. Then on the next > pass thru the foreach, I want $results to be the newer, modified > array. > > This does not seem to work. It appears that the foreach statement > is implemented such that $results is read into memory at the start > so that any modifications I make to it during a given pass, > are ignored > on the next pass. Is this true? > > If so, is there a way that I can tell the foreach statement > to re-read the > array $results? Or am I just going against the grain here? > > -Andres > > > -- > 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
[PHP] modifying within foreach
In the following example: foreach ($results as $key => $item) { //bla bla bla -- unset some of the $items } I want to modify $results within the foreach. In other words, during a given pass of this iteration, I want to delete some of the items based on particular conditions. Then on the next pass thru the foreach, I want $results to be the newer, modified array. This does not seem to work. It appears that the foreach statement is implemented such that $results is read into memory at the start so that any modifications I make to it during a given pass, are ignored on the next pass. Is this true? If so, is there a way that I can tell the foreach statement to re-read the array $results? Or am I just going against the grain here? -Andres -- PHP General Mailing List (http://www.php.net/) To unsubscribe, visit: http://www.php.net/unsub.php