Re: Numerical refs to assoc array suddenly "undefined offset"??

From: Date: Sat, 30 Jun 2001 22:39:55 +0000
Subject: Re: Numerical refs to assoc array suddenly "undefined offset"??
References: 1  Groups: php.general 
Request: Send a blank email to php-general+get-55696@lists.php.net to get a copy of this message
This should not work - at least in that way you'd like. These arrays are associative ones and so, numerical indices are not specified. use foreach ($arr as $key => $val) or list ($key, $val) = each($arr) to traverse an array properly. ----- Original Message ----- From: "CC Zona" <cczona@nospam.invalid> To: <php-general@lists.php.net> Sent: 2001. június 29. 19:12 Subject: [PHP] Numerical refs to assoc array suddenly "undefined offset"?? > This is weird. Did I only imagine that PHP allows numerical referencing of > associative array elements? 'Cuz suddenly the parser is calling all the > numerical references "undefined". > > ex. > $my_arr['foo']='first'; > $my_arr['bar']='second'; > $my_arr['more']='third'; > > echo "<p>" . $my_arr[0] . "</p>\n"; > echo "<p>" . $my_arr[1] . "</p>\n"; > echo "<p>" . $my_arr[2] . "</p>\n"; > > for($i=0;$i<count($my_arr);$i++) > { > echo "<p>" . $my_arr[$i] . "</p>\n"; > } > > ex. > $my_arr=array( > 'foo'=>'first', > 'bar'=>'second', > 'more'=>'third' > ); > > echo "<p>" . $my_arr[0] . "</p>\n"; > echo "<p>" . $my_arr[1] . "</p>\n"; > echo "<p>" . $my_arr[2] . "</p>\n"; > > for($i=0;$i<count($my_arr);$i++) > { > echo "<p>" . $my_arr[$i] . "</p>\n"; > } > > The above spits out nothing but a page full of "undefined offset" errors. > I can't find anything in the arrays chapter, nor in the array function > reference, to explain why the numerical referencing would've stopped > working. This *should* work, right? So why would it not be working now? > > -- > CC > > -- > PHP General Mailing List (http://www.php.net/) > To unsubscribe, e-mail: php-general-unsubscribe@lists.php.net > For additional commands, e-mail: php-general-help@lists.php.net > To contact the list administrators, e-mail: php-list-admin@lists.php.net >

« previous php.general (#55696) next »