161

How can I print the filepath and filename values from each row?

  Array (
    [0] => Array (
             [fid] => 14
             [list] => 1
             [data] => Array (
                         [alt] =>
                         [title] =>
                       )
             [uid] => 1
             [filename] => trucks_10785.jpg
             [filepath] => sites/default/files/trucks_10785.jpg
             [filemime] => image/jpeg
             [filesize] => 143648
             [status] => 1
             [timestamp] => 1291424171
             [nid] => 8
           )
    [1] => Array (
             [fid] => 19
             [list] => 1
             [data] => Array (
                         [alt] =>
                         [title] =>
                       )
             [uid] => 1
             [filename] => school.jpg
             [filepath] => sites/default/files/school.jpg
             [filemime] => image/jpeg
             [filesize] => 115355
             [status] => 1
             [timestamp] => 1292029563
             [nid] => 8
           )
    [2] => Array (
             [fid] => 20
             [list] => 1
             [data] => Array (
                         [alt] =>
                         [title] =>
                       )
             [uid] => 1
             [filename] => Life_is_wonderful_by_iNeedChemicalX.jpg
             [filepath] => sites/default/files/Life_is_wonderful_by_iNeedChemicalX_0.jpg
             [filemime] => image/jpeg
             [filesize] => 82580
             [status] => 1
             [timestamp] => 1292029572
             [nid] => 8
           )
    [3] => Array (
             [fid] => 21
             [list] => 1
             [data] => Array (
                         [alt] =>
                         [title] =>
                       )
             [uid] => 1
             [filename] => school_rural.jpg
             [filepath] => sites/default/files/school_rural.jpg
             [filemime] => image/jpeg
             [filesize] => 375088
             [status] => 1
             [timestamp] => 1292029582
             [nid] => 8
           )
  )
mickmackusa
  • 43,625
  • 12
  • 83
  • 136
esafwan
  • 17,311
  • 33
  • 107
  • 166

6 Answers6

356

Using a foreach loop without a key:

foreach($array as $item) {
    echo $item['filename'];
    echo $item['filepath'];

    // To know what's in $item
    echo '<pre>'; var_dump($item);
}

Using a foreach loop with a key:

foreach($array as $i => $item) {
    echo $array[$i]['filename'];
    echo $array[$i]['filepath'];

    // $array[$i] is same as $item
}

Using a for loop:

for ($i = 0; $i < count($array); $i++) {
    echo $array[$i]['filename'];
    echo $array[$i]['filepath'];
}

var_dump is a really useful function to get a snapshot of an array or object.

rob
  • 8,134
  • 8
  • 58
  • 68
Ish
  • 28,486
  • 11
  • 60
  • 77
  • 3
    Also checkout `var_export`, which prints valid PHP code. You can save that to a file, then write the code to loop over it there before putting it in your main code. – Ben Mar 30 '17 at 15:39
  • is here a problem with using the for loop method. I read somewhere that index might not exist?? – Lightsout Sep 08 '17 at 04:32
  • 1
    If index is not defined like `['a', 'b', 'c']`, then it's values are 0,1 and 2. – Ish Sep 08 '17 at 17:57
14

For more special cases, you also could use this one:

array_map(function($n) { echo $n['filename']; echo $n['filepath'];}, $array);

Or in a more un-complex way:

function printItem($n){
    echo $n['filename'];
    echo $n['filepath'];
}

array_map('printItem', $array);

This will allow you to manipulate the data in an easier way.

Peter Mortensen
  • 30,738
  • 21
  • 105
  • 131
obsergiu
  • 350
  • 3
  • 13
6

Starting simple, with no HTML:

foreach($database as $file) {
    echo $file['filename'] . ' at ' . $file['filepath'];
}

And you can otherwise manipulate the fields in the foreach.

SilverbackNet
  • 2,076
  • 17
  • 29
6
foreach($array as $item => $values) {
    echo $values->filepath;
}
Peter Mortensen
  • 30,738
  • 21
  • 105
  • 131
  • 4
    When answering an eight year old question with three other existing answers it is useful to explain what new approach your answer brings to the question. Also in this case the question was to print "each of the filepath and filename" not to print the just the filepath. – Jason Aller Aug 28 '19 at 04:03
  • 1
    It shoud be echo $values['filepath']; for as $values is an array, not an object. – Daniel Faure Jun 11 '20 at 19:58
0

In modern PHP, array destructuring can be used to access only specific row columns. To print, string concatenation and interpolation can be avoided by using printf() instead of echo.

foreach ($array as ['filename' => $fn, 'filepath' => $fp]) {
    printf("filename: %s, filepath: %s\n", $fn, $fp);
}

It is a misuse of a native function to call array_map() and NOT use its return value. If functional-style iteration is desired, then iterate with array_walk() because its return value is not designed to represent the iterated data.

array_walk($array, fn($row) => 
    printf("filename: %s, filepath: %s\n", $row['filename'], $row['filepath'])
);
mickmackusa
  • 43,625
  • 12
  • 83
  • 136
-4

You can use also this without creating additional variables nor copying the data in the memory like foreach() does.

while (false !== (list($item, $values) = each($array)))
{
    ...
}
  • 4
    each function has been deprecated as of PHP 7.2.0. "Relying on this function is highly discouraged" https://www.php.net/manual/en/function.each.php – asiop Oct 31 '19 at 17:39