循环嵌套数组

时间:2013-02-06 19:23:02

标签: php arrays

我有一个输出数组如下。我想遍历数组,以便我可以一次获得一个文件元素。即一个图像元素及其详细信息。

我尝试了很多但是无法做到。

 Array
    (
        [name] => Array
            (
                [0] => stock-photo-cup-icons-tea-and-coffee-raster-version-109119257.jpg
                [1] => tea-cup-icon.png
                [2] => tea.png
                [3] => stock-vector-vector-black-tea-icons-set-all-white-areas-are-cut-away-from-icons-and-black-areas-merged-81392851.jpg
            )

        [type] => Array
            (
                [0] => image/jpeg
                [1] => image/png
                [2] => image/png
                [3] => image/jpeg
            )

        [tmp_name] => Array
            (
                [0] => D:\xampp\tmp\php6BB.tmp
                [1] => D:\xampp\tmp\php6BC.tmp
                [2] => D:\xampp\tmp\php6BD.tmp
                [3] => D:\xampp\tmp\php6BE.tmp
            )

        [error] => Array
            (
                [0] => 0
                [1] => 0
                [2] => 0
                [3] => 0
            )

        [size] => Array
            (
                [0] => 30609
                [1] => 3615
                [2] => 8966
                [3] => 23117
            )

    )

4 个答案:

答案 0 :(得分:2)

你的意思是什么?

$images = array();
foreach( $array as $key=>$values ){

    foreach( $values as $no=>$value ){
        $images[$no][$key] = $value;
    }
}

答案 1 :(得分:1)

你可以这样做,虽然很糟糕!

for ($i = 0; $i < count($array["name"]); $i++)
{
    echo $array["name"][$i], $array["type"][$i], $array["size"][$i];
}

注意:这仅适用于此类顺序数组。这不适用于具有关联索引的数组。

答案 2 :(得分:0)

你只需循环遍历数组

$count = 0;
foreach ($_FILES['fileField']['name'] as $filename) 
{
    $file = $_FILES['fileField']['name'][$count];
    $type = $_FILES['fileField']['type'][$count];
    $size = $_FILES['fileField']['size'][$count];
    $count++;
}

答案 3 :(得分:0)

我会做这样的事情:

$fileDetais = array();

foreach($arr["name"] as $id => $fname) {
    $fileDetais["name"] = $fname;
    $fileDetais["type"] = $arr["type"][$id];
    $fileDetais["tmp_name"] = $arr["tmp_name"][$id];
    $fileDetais["error"] = $arr["error"][$id];
    $fileDetais["size"] = $arr["size"][$id]; 
    //now you have all info about one file in  $fileDetais 
}