[php] Loop through an array php

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

This question is related to php arrays printing loops

The answer is


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

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

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.


Ok, I know there is an accepted answer but… 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.


Examples related to php

I am receiving warning in Facebook Application using PHP SDK Pass PDO prepared statement to variables Parse error: syntax error, unexpected [ Preg_match backtrack error Removing "http://" from a string How do I hide the PHP explode delimiter from submitted form results? Problems with installation of Google App Engine SDK for php in OS X Laravel 4 with Sentry 2 add user to a group on Registration php & mysql query not echoing in html with tags? How do I show a message in the foreach loop?

Examples related to arrays

PHP array value passes to next row Use NSInteger as array index How do I show a message in the foreach loop? Objects are not valid as a React child. If you meant to render a collection of children, use an array instead Iterating over arrays in Python 3 Best way to "push" into C# array Sort Array of object by object field in Angular 6 Checking for duplicate strings in JavaScript array what does numpy ndarray shape do? How to round a numpy array?

Examples related to printing

How do I print colored output with Python 3? Print a div content using Jquery Python 3 print without parenthesis How to find integer array size in java Differences Between vbLf, vbCrLf & vbCr Constants Printing variables in Python 3.4 Show DataFrame as table in iPython Notebook Removing display of row names from data frame Javascript window.print() in chrome, closing new window or tab instead of cancelling print leaves javascript blocked in parent window Print a div using javascript in angularJS single page application

Examples related to loops

How to increment a letter N times per iteration and store in an array? Angular 2 Cannot find control with unspecified name attribute on formArrays What is the difference between i = i + 1 and i += 1 in a 'for' loop? Prime numbers between 1 to 100 in C Programming Language Python Loop: List Index Out of Range JavaScript: Difference between .forEach() and .map() Why does using from __future__ import print_function breaks Python2-style print? Creating an array from a text file in Bash Iterate through dictionary values? C# Wait until condition is true