php中用foreach来操作数组的代码

前端之家收集整理的这篇文章主要介绍了php中用foreach来操作数组的代码前端之家小编觉得挺不错的,现在分享给大家,也给大家做个参考。

foreach()有两种用法
<div class="codetitle"><a style="CURSOR: pointer" data="6853" class="copybut" id="copybut6853" onclick="doCopy('code6853')"> 代码如下:

<div class="codebody" id="code6853">
foreach(array_name as $value)
{
statement;
}

这里的array_name是你要遍历的数组名,每次循环中,array_name数组的当前元素的值被赋给$value,并且数组内部的下标向下移一 步,也就是下次循环回得到下一个元素。
<div class="codetitle"><a style="CURSOR: pointer" data="98567" class="copybut" id="copybut98567" onclick="doCopy('code98567')"> 代码如下:
<div class="codebody" id="code98567">
foreach(array_name as $key => $value)
{
statement;
}

这里跟第一种方法的区别就是多了个$key,也就是除了把当前元素的值赋给$value外,当前元素的键值也会在每次循环中被赋给变量$key。键值可以 是下标值,也可以是字符串。比如book[0]=1中的"0",book[id]="001"中的"id".
程序实例1:
<div class="codetitle"><a style="CURSOR: pointer" data="76541" class="copybut" id="copybut76541" onclick="doCopy('code76541')"> 代码如下:
<div class="codebody" id="code76541">
<?PHP
/-------------------------------------------------------------------------/
/ foreach example 1: value only /
echo "foreach example 1: value only ".'
';
$a = array(1,2,3,17);
foreach ($a as $v) {
echo "Current value of ".$a.":". $v."
";
}
?>
// 运行结果
foreach example 1: value only
Current value of $a: 1
Current value of $a: 2
Current value of $a: 3
Current value of $a: 17

2
<div class="codetitle"><a style="CURSOR: pointer" data="16062" class="copybut" id="copybut16062" onclick="doCopy('code16062')"> 代码如下:
<div class="codebody" id="code16062">
/-------------------------------------------------------------------------/
/ foreach example 2: value (with key printed for illustration) /
echo '
'.'
'."foreach example 2: value (with key printed for illustration) ".'
';
$a = array(1,17);
$i = 0; / for illustrative purposes only /
foreach ($a as $v) {
echo ""$a[$i] => $v".'
';
$i++;
}
// 程序运行结果
foreach example 2: value (with key printed for illustration)
$a[0] => 1
$a[1] => 2
$a[2] => 3
$a[3] => 17

3
<div class="codetitle"><a style="CURSOR: pointer" data="97499" class="copybut" id="copybut97499" onclick="doCopy('code97499')"> 代码如下:<div class="codebody" id="code97499">
/-------------------------------------------------------------------------/
/ foreach example 3: key and value /
echo '
'.'
'."foreach example 3: key and value ".'
';
$a = array(
"one" => 1,
"two" => 2,
"three" => 3,
"seventeen" => 17
);
foreach ($a as $k => $v) {
echo ""$a[$k] => $v".'
';
}
// 程序运行结果
foreach example 3: key and value
$a[one] => 1
$a[two] => 2
$a[three] => 3
$a[seventeen] => 17

4
<div class="codetitle"><a style="CURSOR: pointer" data="79746" class="copybut" id="copybut79746" onclick="doCopy('code79746')"> 代码如下:<div class="codebody" id="code79746">
/-------------------------------------------------------------------------/
/ foreach example 4: multi-dimensional arrays /
echo '
'.'
'."foreach example 4: multi-dimensional arrays ".'
';
$a = array();
$a[0][0] = "a";
$a[0][1] = "b";
$a[1][0] = "y";
$a[1][1] = "z";
foreach ($a as $v1) {
foreach ($v1 as $v2) {
echo "$v2"n";
}
}
// 程序运行结果
foreach example 4: multi-dimensional arrays
a b y z

5
<div class="codetitle"><a style="CURSOR: pointer" data="6449" class="copybut" id="copybut6449" onclick="doCopy('code6449')"> 代码如下:<div class="codebody" id="code6449">
/-------------------------------------------------------------------------/
/ foreach example 5: dynamic arrays /
echo '
'.'
'."foreach example 5: dynamic arrays ".'
';
foreach (array(1,4,5) as $v) {
echo "$v"n";
}
// 程序运行结果
foreach example 5: dynamic arrays
1 2 3 4 5

也可以这么用:
<div class="codetitle"><a style="CURSOR: pointer" data="26339" class="copybut" id="copybut26339" onclick="doCopy('code26339')"> 代码如下:<div class="codebody" id="code26339">
$messageNav['首页'] =ROOT_PATH;
$messageNav['人才交流'] ="#"
$messageNav['动态专栏'] ="hragent/cn/"
<?php $i = 0;foreach ($messageNav as $key=>$value):?>
<?php if ($i != count($messageNav) - 1):?>
<a href="<?=$value?>"><?=$key?>>
<?php else:?>
<a href="<?=$value?>" class="onlink"><?=$key?>
<?php endif;?>
<?php $i++;endforeach;?>

猜你在找的PHP相关文章