Re: dynamic/multidimensional arrays in classes
| From: | Jason k Larson | Date: | Fri, 28 Mar 2003 13:03:17 +0000 |
| Subject: | Re: dynamic/multidimensional arrays in classes | ||
| References: | 1 2 3 4 5 6 | Groups: | php.general |
| Request: | Send a blank email to php-general+get-141432@lists.php.net to get a copy of this message | ||
The problem here lies with the each function, I believe. I played with this for a minute and that's where I ran into problems with unexpected behaviour. Regardless, I was able to get expected functionality using foreach instead of the while loop. While foreach isn't available in PHP3 it is in all versions of PHP4. If you can't use foreach, writing the while loop differently (not using each()) should work better.
HTH,
Jason k Larson
Patrick Teague wrote:
I'm currently having a problem with another array in a class & I can't figure it out - class myClass {function myClass($arr = array( 'item1'=>array('a','b','c'),'item2'=>array('a','b','c') ) ){ print_r( $arr ); while( list($key,$val) = each($arr) ) { print($key."=".$val); } }} now, if I say $mine = myClass(); it only prints the print_r statement, but not the print($key."=".$val) statment meaning it doesn't even bother entering the loop. However if I say $mine = myClass(array( 'item1'=>array('a','b','c'), 'item2'=>array('a','b','c') ) ); it prints both... is there some reason why I need to repeat the default value when creating the class? I've also tried using a "" string on class creation, but no luck there either. Patrick