欢迎您访问程序员文章站本站旨在为大家提供分享程序员计算机编程知识!
您现在的位置是: 首页  >  php教程

PHP 数组处理使用foreach、list、each等三个函数详解

程序员文章站 2022-05-08 15:54:01
...
本文介绍PHP中遍历数组的foreach、list、each三个函数的使用方法:

方法1:foreach
$sports = array(
football => good,
swimming => very well,
running => not good);
foreach ($sports as $key => $value) {
echo $key.": ".$value."
";
?>

输出结果:
football: good
swimming: very well
running: not good

方法2:each
$sports = array(
football => good,
swimming => very well,
running => not good);
while ($elem = each($sports)) {
echo $elem[key].": ".$elem[value]."
";
?>

方法3:list & each
$sports = array(
football => good,
swimming => very well,
running => not good);
while (list($key, $value) = each($sports)) {
echo $key.": ".$value."
";
?>