Re: foreach function
| From: | Dean Hall | Date: | Sat, 14 Apr 2001 01:36:33 +0000 |
| Subject: | Re: foreach function | ||
| References: | 1 | Groups: | php.general |
| Request: | Send a blank email to php-general+get-48502@lists.php.net to get a copy of this message | ||
""kenny.hibs"" <kenny.hibs@ntlworld.com> wrote:
> My isp uses php3 and some of my scripts have the function 'foreach'
> which is php4.
> Is there are way to fix this
Sure.
If your code says:
foreach($array as $element) { // this is a numerically-indexed array
...
}
do:
while(list(,$element) = each($array)) { // the "," is important in
"list(,$element)".
...
}
If your code says:
foreach($array as $key => $value) { // real associative array
...
}
do:
while($list($key, $value) = each($array)) {
...
}
This way, all you have to change is the loop itself -- nothing inside the
loop needs to be changed if you do it this way.
Dean.