foreach就是遍历数组的呀foreach ($array as $key => $value)
 echo "$key => $value"
$key为健,$value为值

解决方案 »

  1.   

    这个函数在PHP中文手册上怎么没有看过
      

  2.   

    http://www.php.net/manual/zh/control-structures.foreach.php
      

  3.   

    你的PHP手册太太老了`~!
    找新的看吧最好当然是英文的
      

  4.   

    foreach
    PHP 4 (not PHP 3) includes a foreach construct, much like Perl and some other languages. This simply gives an easy way to iterate over arrays. foreach works only on arrays, and will issue an error when you try to use it on a variable with a different data type or an uninitialized variables. There are two syntaxes; the second is a minor but useful extension of the first: 
    foreach(array_expression as $value) statement
    foreach(array_expression as $key => $value) statement
     The first form loops over the array given by array_expression. On each loop, the value of the current element is assigned to $value and the internal array pointer is advanced by one (so on the next loop, you'll be looking at the next element). The second form does the same thing, except that the current element's key will be assigned to the variable $key on each loop. 
    Note: When foreach first starts executing, the internal array pointer is automatically reset to the first element of the array. This means that you do not need to call reset() before a foreach loop. Note: Also note that foreach operates on a copy of the specified array, not the array itself, therefore the array pointer is not modified as with the each() construct and changes to the array element returned are not reflected in the original array. However, the internal pointer of the original array is advanced with the processing of the array. Assuming the foreach loop runs to completion, the array's internal pointer will be at the end of the array. 
    Note: foreach does not support the ability to suppress error messages using '@'. You may have noticed that the following are functionally identical: 
    reset ($arr);
    while (list(, $value) = each ($arr)) {
        echo "Value: $value<br>\n";
    }foreach ($arr as $value) {
        echo "Value: $value<br>\n";
    }
     
    The following are also functionally identical: reset ($arr);
    while (list($key, $value) = each ($arr)) {
        echo "Key: $key; Value: $value<br>\n";
    }foreach ($arr as $key => $value) {
        echo "Key: $key; Value: $value<br>\n";
    }
     Some more examples to demonstrate usages: 
    /* foreach example 1: value only */$a = array (1, 2, 3, 17);foreach ($a as $v) {
       print "Current value of \$a: $v.\n";
    }/* foreach example 2: value (with key printed for illustration) */$a = array (1, 2, 3, 17);$i = 0; /* for illustrative purposes only */foreach($a as $v) {
        print "\$a[$i] => $v.\n";
        $i++;
    }/* foreach example 3: key and value */$a = array (
        "one" => 1,
        "two" => 2,
        "three" => 3,
        "seventeen" => 17
    );foreach($a as $k => $v) {
        print "\$a[$k] => $v.\n";
    }/* foreach example 4: multi-dimensional arrays */$a[0][0] = "a";
    $a[0][1] = "b";
    $a[1][0] = "y";
    $a[1][1] = "z";foreach($a as $v1) {
        foreach ($v1 as $v2) {
            print "$v2\n";
        }
    }/* foreach example 5: dynamic arrays */foreach(array(1, 2, 3, 4, 5) as $v) {
        print "$v\n";
    }
      

  5.   

    中文对照,翻译效果不太好foreach
    PHP 4(不是 PHP 3)包括了 foreach 结构,和 Perl 以及其他语言很像。这只是一种遍历数组简便方法。foreach 仅能用于数组,当试图将其用于其它数据类型或者一个未初始化的变量时会产生错误。有两种语法,第二种比较次要但却是第一种的有用的扩展。 
    foreach(array_expression as $value) statement
    foreach(array_expression as $key => $value) statement
     第一种格式遍历给定的 array_expression 数组。每次循环中,当前单元的值被赋给 $value 并且数组内部的指针向前移一步(因此下一次循环中将会得到下一个单元)。 第二种格式做同样的事,只除了当前单元的键值也会在每次循环中被赋给变量 $key。 
    注: 当 foreach 开始执行时,数组内部的指针会自动指向第一个单元。这意味着不需要在 foreach 循环之前调用 reset()。 注: 此外注意 foreach 所操作的是指定数组的一个拷贝,而不是该数组本身。因此即使有 each() 的构造,原数组指针也没有变,数组单元的值也不受影响。 
    注: foreach 不支持用“@”来禁止错误信息的能力。 你可能注意到了以下的代码功能完全相同: 
    reset ($arr);
    while (list(, $value) = each ($arr)) {
        echo "Value: $value<br>\n";
    }foreach ($arr as $value) {
        echo "Value: $value<br>\n";
    }
     
    以下代码功能也完全相同: reset ($arr);
    while (list($key, $value) = each ($arr)) {
        echo "Key: $key; Value: $value<br>\n";
    }foreach ($arr as $key => $value) {
        echo "Key: $key; Value: $value<br>\n";
    }
     示范用法的更多例子: 
    /* foreach example 1: value only */$a = array (1, 2, 3, 17);foreach ($a as $v) {
       print "Current value of \$a: $v.\n";
    }/* foreach example 2: value (with key printed for illustration) */$a = array (1, 2, 3, 17);$i = 0; /* for illustrative purposes only */foreach($a as $v) {
        print "\$a[$i] => $v.\n";
        $i++;
    }/* foreach example 3: key and value */$a = array (
        "one" => 1,
        "two" => 2,
        "three" => 3,
        "seventeen" => 17
    );foreach($a as $k => $v) {
        print "\$a[$k] => $v.\n";
    }/* foreach example 4: multi-dimensional arrays */$a[0][0] = "a";
    $a[0][1] = "b";
    $a[1][0] = "y";
    $a[1][1] = "z";foreach($a as $v1) {
        foreach ($v1 as $v2) {
            print "$v2\n";
        }
    }/* foreach example 5: dynamic arrays */foreach(array(1, 2, 3, 4, 5) as $v) {
        print "$v\n";
    }