php - 多维数组foreach只返回一个结果

时间:2013-03-22 15:16:49

标签: php arrays multidimensional-array

我是php的初学者,我有一个变量,有关上传图片的sotres信息

我得到一个多维数组

Array
(
    [0] => Array
        (
            [errors] => Array
                (
                )

            [path] => addons/uploads/albums/1/1/rDtKgyVAvTjSkLA.jpg
            [filename] => rDtKgyVAvTjSkLA.jpg
            [original_name] => rachaelCache_5750270_thm.jpg
            [resizes] => Array
                (
                    [0] => 1
                    [1] => 1
                )

        )

    [1] => Array
        (
            [errors] => Array
                (
                )

            [path] => addons/uploads/albums/1/1/qTLglBgAPxvDFtr.png
            [filename] => qTLglBgAPxvDFtr.png
            [original_name] => Screen Shot 2013-03-02 at 11.28.48 AM.png
            [resizes] => Array
                (
                    [0] => 1
                    [1] => 1
                )

        )

)

但是当我预告它时我只会得到一个结果

foreach ($upload as $row) {
   echo $row['filename'];
}

可以请别人告诉我为什么会这样?

1 个答案:

答案 0 :(得分:0)

将您的代码重新创建为live-demo,效果很好:---> http://3v4l.org/986e7

$upload = array(array(
        'errors' => array(), 
        'filename' => '/some/path/to/a/file.jpg'), 
    array(
        'errors' => array(), 
        'filename' => '/yet/another/path.jpg'));

var_dump($upload);

foreach ($upload as $x) 
    echo $x['filename'],"<br>";

echo $upload[0]['filename'];

要找出问题所在,我会在var_dump($upload);循环之前设置foreach,以查看$ upload是否包含您认为的内容。