Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Not able to fetch correct value from array

I wish to fetch values from the following array

echo "<pre>";
print_r($attachments); 
echo "</pre>";


    Array
    (
        [0] => Array
            (
                [is_attachment] => 
                [filename] =>  Desert.jpg
                [name] =>  Desert.jpg
                [attachment] => 
            )

        [1] => Array
            (
                [is_attachment] => 1
                [filename] => Hydrangeas.jpg
                [name] =>  Hydrangeas.jpg
                [attachment] => 
            )
    )

I used this code to fetch the values

foreach($attachments as $value)
    { 
        foreach($value as $values)
            { 
                echo $values[filename];
                echo "<br>";
            } 
    }

O/P that i got is

D
D
H
H

The desired o/p should be

 Desert.jpg
 Hydrangeas.jpg

Can anyone tell where i went wrong


1 Answers

No need of two loops, you can get values from single loop as it's outer loop of [0],[1] etc.

foreach($attachments as $value)
    { 
        echo $value['filename'];
                echo "<br>"; 
    }
like image 88
Disha V. Avatar answered Apr 19 '26 00:04

Disha V.