因为Foreach是数组遍历。
你没有2这个Key。所以不会出现你所说的情况

解决方案 »

  1.   

    给你段代码,自己输出看看
    /* 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";
        }
    }
      

  2.   

    那用asort可以完成怎样的效果?
    ksort那?
      

  3.   

    这是php manual中asort和ksort,你看看手册中有你要的问题吗?其实,不必过分追求什么原理,有些东西自己试试完全可以了解的。如果你真的想知道到底能不能,还是看函数的源码吧!ksort
    (PHP 3, PHP 4 )ksort -- Sort an array by key
    Description
    int ksort ( array array [, int sort_flags])
    Sorts an array by key, maintaining key to data correlations. This is useful mainly for associative arrays. Example 1. ksort() example$fruits = array ("d"=>"lemon", "a"=>"orange", "b"=>"banana", "c"=>"apple");
    ksort ($fruits);
    reset ($fruits);
    while (list ($key, $val) = each ($fruits)) {
        echo "$key = $val\n";
    }
     
     
    This example would display: a = orange
    b = banana
    c = apple
    d = lemon
     
    You may modify the behavior of the sort using the optional parameter sort_flags, for details see sort(). See also asort(), arsort(), krsort(), uksort(), sort(), natsort(), and rsort(). Note: The second parameter was added in PHP 4. 
    asort
    (PHP 3, PHP 4 )asort -- Sort an array and maintain index association
    Description
    void asort ( array array [, int sort_flags])
    This function sorts an array such that array indices maintain their correlation with the array elements they are associated with. This is used mainly when sorting associative arrays where the actual element order is significant. Example 1. asort() example$fruits = array ("d"=>"lemon", "a"=>"orange", "b"=>"banana", "c"=>"apple");
    asort ($fruits);
    reset ($fruits);
    while (list ($key, $val) = each ($fruits)) {
        echo "$key = $val\n";
    }
     This example would display: c = apple
    b = banana
    d = lemon
    a = orange
      The fruits have been sorted in alphabetical order, and the index associated with each element has been maintained. You may modify the behavior of the sort using the optional parameter sort_flags, for details see sort(). See also arsort(), rsort(), ksort(), and sort().