note 15485 deleted from control-structures.foreach by sniper
| From: | sniper@php.net | Date: | Wed, 08 Oct 2003 04:11:45 +0000 |
| Subject: | note 15485 deleted from control-structures.foreach by sniper | ||
| References: | 1 | Groups: | php.notes |
| Request: | Send a blank email to php-notes+get-58018@lists.php.net to get a copy of this message | ||
Note Submitter: danDOESNTWANTANYSPAMstowell@buzzle.com
----
"david@petshelter.net" mentioned that using foreach() on an associative array goes through
each element twice. Here's a hack to get around that. Instead of...
foreach($myarray as $key => $value)
echo "Key: $key; Value: $value<br />\n";
...use...
foreach($myarray as $key => $value)
if(strval(intval($key))!=$key)
echo "Key: $key; Value: $value<br />\n";
This then only acts on the associative keys. If you want to use only the numerical keys, change the
comparator in the IF from "!=" to "==".
Hope this helps!
Dan (www.mcld.co.uk)