php遍历数组的方法分享_PHP教程
程序员文章站
2024-02-06 08:58:52
...
在PHP中数组分为两类: 数字索引数组和关联数组。
其中数字索引数组和C语言中的数组一样,下标是为0,1,2…
而关联数组下标可能是任意类型,与其它语言中的hash,map等结构相似。
方法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']."
";
}
?>
输出结果:
football: good
swimming: very well
running: not good
方法3:list & each
$sports = array(
'football' => 'good',
'swimming' => 'very well',
'running' => 'not good');
while (!!list($key, $value) = each($sports)) {
echo $key.": ".$value."
";
}
?>
输出结果:
football: good
swimming: very well
running: not good
其中数字索引数组和C语言中的数组一样,下标是为0,1,2…
而关联数组下标可能是任意类型,与其它语言中的hash,map等结构相似。
方法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']."
";
}
?>
输出结果:
football: good
swimming: very well
running: not good
方法3:list & each
复制代码 代码如下:
$sports = array(
'football' => 'good',
'swimming' => 'very well',
'running' => 'not good');
while (!!list($key, $value) = each($sports)) {
echo $key.": ".$value."
";
}
?>
输出结果:
football: good
swimming: very well
running: not good
推荐阅读
-
php获取从html表单传递数组的方法_PHP教程
-
php遍历数组的方法分享_PHP教程
-
PHP使用strstr()函数获取指定字符串后所有字符的方法,strstr字符串_PHP教程
-
ThinkPHP3.2.3实现分页的方法详解,thinkphp3.2.3分页_PHP教程
-
令你的网站获得任意Google PR值的方法_PHP教程
-
apache php gzip压缩输出的实现方法,apachegzip_PHP教程
-
elcomsoft wireless security au PHP中的session永不过期的解决思路及实现方法分享
-
php中使用url传递数组的方法,phpurl传递数组_PHP教程
-
PHP 将json的stdClass Object转成数组array的方法
-
php实现阿拉伯数字和罗马数字相互转换的方法,阿拉伯罗马数字_PHP教程