search
Homephp教程php手册PHP用foreach遍历数组比较高效

foreach()有两种用法:

foreach(array_name as $value)    
{       
	statement;    
}

这里的array_name是你要遍历的数组名,每次循环中,array_name数组的当前元素的值被赋给$value,并且数组内部的下标向下移一 步,也就是下次循环回得到下一个元素。

foreach(array_name as $key => $value)    
{        
	statement;      
} 

这里跟第一种方法的区别就是多了个$key,也就是除了把当前元素的值赋给$value外,当前元素的键值也会在每次循环中被赋给变量$key。键值可以 是下标值,也可以是字符串。比如book[0]=1中的"0",book[id]="001"中的"id".

程序实例:

1

<?php
/*-------------------------------------------------------------------------*/
/* foreach example 1: value only */
echo "foreach example 1: value only ".'<br />';
$a = array(1, 2, 3, 17);
foreach ($a as $v) {
   echo "Current value of ".$a.":". $v."<br />";
}
?>
// 运行结果
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

/*-------------------------------------------------------------------------*/
/* foreach example 2: value (with key printed for illustration) */
echo '<br />'.'<br />'."foreach example 2: value (with key printed for illustration) ".'<br />';
$a = array(1, 2, 3, 17);
$i = 0; /* for illustrative purposes only */
foreach ($a as $v) {
    echo ""$a[$i] => $v".'<br />';
    $i++;
}
// 程序运行结果
foreach example 2: value (with key printed for illustration) 
$a[0] => 1
$a[1] => 2
$a[2] => 3
$a[3] => 17

3

/*-------------------------------------------------------------------------*/
/* foreach example 3: key and value */
echo '<br />'.'<br />'."foreach example 3: key and value ".'<br />';
$a = array(
    "one" => 1,
    "two" => 2,
    "three" => 3,
    "seventeen" => 17
);
foreach ($a as $k => $v) {
    echo ""$a[$k] => $v".'<br />';
}
// 程序运行结果
foreach example 3: key and value 
$a[one] => 1
$a[two] => 2
$a[three] => 3
$a[seventeen] => 17

4

/*-------------------------------------------------------------------------*/
/* foreach example 4: multi-dimensional arrays */
echo '<br />'.'<br />'."foreach example 4: multi-dimensional arrays ".'<br />';
$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

/*-------------------------------------------------------------------------*/
/* foreach example 5: dynamic arrays */
echo '<br />'.'<br />'."foreach example 5: dynamic arrays ".'<br />';
foreach (array(1, 2, 3, 4, 5) as $v) {
    echo "$v"n";
}
// 程序运行结果
foreach example 5: dynamic arrays 
1 2 3 4 5

也可以这么用:

$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?></a>> 
<?php else:?>
<a href="<?=$value?>" class="onlink"><?=$key?></a>
<?php endif;?>
<?php $i++;endforeach;?>
Statement
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn

Hot AI Tools

Undresser.AI Undress

Undresser.AI Undress

AI-powered app for creating realistic nude photos

AI Clothes Remover

AI Clothes Remover

Online AI tool for removing clothes from photos.

Undress AI Tool

Undress AI Tool

Undress images for free

Clothoff.io

Clothoff.io

AI clothes remover

Video Face Swap

Video Face Swap

Swap faces in any video effortlessly with our completely free AI face swap tool!

Hot Tools

SublimeText3 Mac version

SublimeText3 Mac version

God-level code editing software (SublimeText3)

Zend Studio 13.0.1

Zend Studio 13.0.1

Powerful PHP integrated development environment

PhpStorm Mac version

PhpStorm Mac version

The latest (2018.2.1) professional PHP integrated development tool

SecLists

SecLists

SecLists is the ultimate security tester's companion. It is a collection of various types of lists that are frequently used during security assessments, all in one place. SecLists helps make security testing more efficient and productive by conveniently providing all the lists a security tester might need. List types include usernames, passwords, URLs, fuzzing payloads, sensitive data patterns, web shells, and more. The tester can simply pull this repository onto a new test machine and he will have access to every type of list he needs.

SublimeText3 English version

SublimeText3 English version

Recommended: Win version, supports code prompts!