note 15485 added to control-structures.foreach

From: Date: Thu, 13 Sep 2001 14:46:36 +0000
Subject: note 15485 added to control-structures.foreach
Groups: php.notes 
Request: Send a blank email to php-notes+get-17978@lists.php.net to get a copy of this message
"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) http://www.php.net/manual/en/control-structures.foreach.php

« previous php.notes (#17978) next »