Re: [PHP] I beleive I have found a bug.....
From: Brian White <[EMAIL PROTECTED]> Date: Wed, Aug 29, 2001 at 06:16:49PM +1000 Message-ID: <[EMAIL PROTECTED]> Subject: Re: [PHP] I beleive I have found a bug. > You didn't read my code closely enough: > > I said: > > > If I replace the problem line > > > foreach ( array_keys($data) as $key2 ) > > >^^ > > > with the alternate > > > foreach ( $data as $key2=>$val2 ) Oops... Sorry! That's what I said... I didn't take a very good look. But now I have... You need to reset the $data-array before the inner foreach-loop. Then there's no problem. --- PHP code --- \n"; foreach ( $data as $key1=>$val1 ) { reset ($data); // <- R E S E T !!! foreach ( array_keys($data) as $key2 ) { if ( $key1 == $key2 ) print "$key2"; else print $key2; print " "; } print "\n"; } print ""; ?> --- End of PHP code --- -- * R&zE: -- -- Renze Munnik -- DataLink BV -- -- E: [EMAIL PROTECTED] -- W: +31 23 5326162 -- F: +31 23 5322144 -- M: +31 6 21811143 -- -- Stationsplein 82 -- 2011 LM HAARLEM -- Netherlands -- -- http://www.datalink.nl -- -- PHP General Mailing List (http://www.php.net/) To unsubscribe, e-mail: [EMAIL PROTECTED] For additional commands, e-mail: [EMAIL PROTECTED] To contact the list administrators, e-mail: [EMAIL PROTECTED]
Re: [PHP] I beleive I have found a bug.....
Renze Munnik said: >I don't _think_ this is a bug (note: think). I haven't taken a very >good look at your code, but as I understand you believe that: > > foreach ($data as $key) > >delivers the same $key as in: > > foreach ($data as $key=>$value) You didn't read my code closely enough: I said: > > If I replace the problem line > > foreach ( array_keys($data) as $key2 ) > >^^ > > with the alternate > > foreach ( $data as $key2=>$val2 ) At 09:54 29/08/2001 +0200, * R&zE: wrote: > >From: Brian White <[EMAIL PROTECTED]> >Date: Wed, Aug 29, 2001 at 04:01:06PM +1000 >Message-ID: <[EMAIL PROTECTED]> >Subject: [PHP] I beleive I have found a bug. > > > ... I just thought I would see if anyone else can reproduce it! > > > > Ok. The problem is in the following snippet of code. ( Note : this exmaple > > is extremely contrived, and is the essence of a much more complicated > > situation involving a function call from a class method... ) > > > > > $data = array( "A" => "AA" , "B" => "BB", "C"=> "CC", "D"=> "DD", "E" > > => "EE" ); > > > > print "\n"; > > foreach ( $data as $key1=>$val1 ) > > { > > //foreach ( $data as $key2=>$val2 ) > > foreach ( array_keys($data) as $key2 ) // <= PROBLEM IS HERE > > { > > if ( $key1 == $key2 ) > > print "$key2"; > > else > > print $key2; > > > > print " "; > > } > > print "\n"; > > } > > > > print ""; > > ?> > > > > I beleive it should generate > > > > A B C D E > > A B C D E > > A B C D E > > A B C D E > > A B C D E > > > > > > Instead it generates > > > > A B C D E > > > > > > If I replace the problem line > > foreach ( array_keys($data) as $key2 ) > > > > with the alternate > > foreach ( $data as $key2=>$val2 ) > > > > then it works fine. > > > > I am using PHP 4.0.4pl1. > > > > > > Regs > > > > Brian White > > > > - > > Brian White > > Step Two Designs Pty Ltd - SGML, XML & HTML Consultancy > > Phone: +612-93197901 > > Web: http://www.steptwo.com.au/ > > Email: [EMAIL PROTECTED] > > > > > > -- > > PHP General Mailing List (http://www.php.net/) > > To unsubscribe, e-mail: [EMAIL PROTECTED] > > For additional commands, e-mail: [EMAIL PROTECTED] > > To contact the list administrators, e-mail: [EMAIL PROTECTED] > > > > > >Hai Brain, > >I don't _think_ this is a bug (note: think). I haven't taken a very >good look at your code, but as I understand you believe that: > > foreach ($data as $key) > >delivers the same $key as in: > > foreach ($data as $key=>$value) > >Well... it doesn't. Check the manual on it. If you use the >foreach ($x as $y) construction, $y is the value... not the key. >Take a look at: > >http://www.php.net/manual/en/control-structures.foreach.php > > >foreach(array_expression as $value) >foreach(array_expression as $key => $value) > > >I think this solves your problem... > > > >-- > >* R&zE: > > >-- >-- Renze Munnik >-- DataLink BV >-- >-- E: [EMAIL PROTECTED] >-- W: +31 23 5326162 >-- F: +31 23 5322144 >-- M: +31 6 21811143 >-- >-- Stationsplein 82 >-- 2011 LM HAARLEM >-- Netherlands >-- >-- http://www.datalink.nl >-- > >-- >PHP General Mailing List (http://www.php.net/) >To unsubscribe, e-mail: [EMAIL PROTECTED] >For additional commands, e-mail: [EMAIL PROTECTED] >To contact the list administrators, e-mail: [EMAIL PROTECTED] - Brian White Step Two Designs Pty Ltd - SGML, XML & HTML Consultancy Phone: +612-93197901 Web: http://www.steptwo.com.au/ Email: [EMAIL PROTECTED] -- PHP General Mailing List (http://www.php.net/) To unsubscribe, e-mail: [EMAIL PROTECTED] For additional commands, e-mail: [EMAIL PROTECTED] To contact the list administrators, e-mail: [EMAIL PROTECTED]
Re: [PHP] I beleive I have found a bug.....
From: Brian White <[EMAIL PROTECTED]> Date: Wed, Aug 29, 2001 at 04:01:06PM +1000 Message-ID: <[EMAIL PROTECTED]> Subject: [PHP] I beleive I have found a bug. > ... I just thought I would see if anyone else can reproduce it! > > Ok. The problem is in the following snippet of code. ( Note : this exmaple > is extremely contrived, and is the essence of a much more complicated > situation involving a function call from a class method... ) > > $data = array( "A" => "AA" , "B" => "BB", "C"=> "CC", "D"=> "DD", "E" > => "EE" ); > > print "\n"; > foreach ( $data as $key1=>$val1 ) > { > //foreach ( $data as $key2=>$val2 ) > foreach ( array_keys($data) as $key2 ) // <= PROBLEM IS HERE > { > if ( $key1 == $key2 ) > print "$key2"; > else > print $key2; > > print " "; > } > print "\n"; > } > > print ""; > ?> > > I beleive it should generate > > A B C D E > A B C D E > A B C D E > A B C D E > A B C D E > > > Instead it generates > > A B C D E > > > If I replace the problem line > foreach ( array_keys($data) as $key2 ) > > with the alternate > foreach ( $data as $key2=>$val2 ) > > then it works fine. > > I am using PHP 4.0.4pl1. > > > Regs > > Brian White > > - > Brian White > Step Two Designs Pty Ltd - SGML, XML & HTML Consultancy > Phone: +612-93197901 > Web: http://www.steptwo.com.au/ > Email: [EMAIL PROTECTED] > > > -- > PHP General Mailing List (http://www.php.net/) > To unsubscribe, e-mail: [EMAIL PROTECTED] > For additional commands, e-mail: [EMAIL PROTECTED] > To contact the list administrators, e-mail: [EMAIL PROTECTED] Hai Brain, I don't _think_ this is a bug (note: think). I haven't taken a very good look at your code, but as I understand you believe that: foreach ($data as $key) delivers the same $key as in: foreach ($data as $key=>$value) Well... it doesn't. Check the manual on it. If you use the foreach ($x as $y) construction, $y is the value... not the key. Take a look at: http://www.php.net/manual/en/control-structures.foreach.php foreach(array_expression as $value) foreach(array_expression as $key => $value) I think this solves your problem... -- * R&zE: -- -- Renze Munnik -- DataLink BV -- -- E: [EMAIL PROTECTED] -- W: +31 23 5326162 -- F: +31 23 5322144 -- M: +31 6 21811143 -- -- Stationsplein 82 -- 2011 LM HAARLEM -- Netherlands -- -- http://www.datalink.nl -- -- PHP General Mailing List (http://www.php.net/) To unsubscribe, e-mail: [EMAIL PROTECTED] For additional commands, e-mail: [EMAIL PROTECTED] To contact the list administrators, e-mail: [EMAIL PROTECTED]