prakashchhetri
prakashchhetri

Reputation: 1816

Looping through nested array

I have a output array as below. I want to loop through the array so that I could get one file element at once. i.e one image element with its details at a time.

I tried a lot but couldnt do it.

 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
            )

    )

Upvotes: 0

Views: 107

Answers (4)

Andreas
Andreas

Reputation: 2678

Do you mean something like that?

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

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

Upvotes: 2

Viktor S.
Viktor S.

Reputation: 12815

I would do something like this:

$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 
}

Upvotes: 0

Ronnie
Ronnie

Reputation: 11198

You just loop through the array like so

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

Upvotes: 0

Praveen Kumar Purushothaman
Praveen Kumar Purushothaman

Reputation: 167230

You can do this way, although it is crappy!

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

Note: This works only for this kind of sequential array. This doesn't apply for arrays with associative index.

Upvotes: 1

Related Questions