What is the best way to loop through this array in PHP?

PhpArraysPrintingLoops

Php Problem Overview


I have this array... how do you print each of the filepath and filename? What is the best way to do this?

  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
           )
  )

Php Solutions


Solution 1 - Php

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.

Solution 2 - Php

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.

Solution 3 - Php

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.

Solution 4 - Php

foreach($array as $item => $values) {
    echo $values->filepath;
}

Solution 5 - Php

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)))
{
    ...
}

Attributions

All content for this solution is sourced from the original question on Stackoverflow.

The content on this page is licensed under the Attribution-ShareAlike 4.0 International (CC BY-SA 4.0) license.

Content TypeOriginal AuthorOriginal Content on Stackoverflow
QuestionesafwanView Question on Stackoverflow
Solution 1 - PhpIshView Answer on Stackoverflow
Solution 2 - PhpobsergiuView Answer on Stackoverflow
Solution 3 - PhpSilverbackNetView Answer on Stackoverflow
Solution 4 - PhpKavinda HarshanaView Answer on Stackoverflow
Solution 5 - PhpjakoubekczView Answer on Stackoverflow