请问如何通过数组php循环

通过数组php循环

我有这个阵列.。如何打印每个文件路径和文件名?做这件事最好的方法是什么?

  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 
           )


九州编程
浏览 368回答 3
3回答

LEATH

使用foreach无键循环foreach($array&nbsp;as&nbsp;$item)&nbsp;{ &nbsp;&nbsp;&nbsp;&nbsp;echo&nbsp;$item['filename']; &nbsp;&nbsp;&nbsp;&nbsp;echo&nbsp;$item['filepath']; &nbsp;&nbsp;&nbsp;&nbsp;//&nbsp;to&nbsp;know&nbsp;what's&nbsp;in&nbsp;$item &nbsp;&nbsp;&nbsp;&nbsp;echo&nbsp;'<pre>';&nbsp;var_dump($item);}使用foreach带密钥的循环foreach($array&nbsp;as&nbsp;$i&nbsp;=>&nbsp;$item)&nbsp;{ &nbsp;&nbsp;&nbsp;&nbsp;echo&nbsp;$array[$i]['filename']; &nbsp;&nbsp;&nbsp;&nbsp;echo&nbsp;$array[$i]['filepath']; &nbsp;&nbsp;&nbsp;&nbsp;//&nbsp;$array[$i]&nbsp;is&nbsp;same&nbsp;as&nbsp;$item}使用for环路for&nbsp;($i&nbsp;=&nbsp;0;&nbsp;$i&nbsp;<&nbsp;count($array);&nbsp;$i++)&nbsp;{ &nbsp;&nbsp;&nbsp;&nbsp;echo&nbsp;$array[$i]['filename']; &nbsp;&nbsp;&nbsp;&nbsp;echo&nbsp;$array[$i]['filepath'];}var_dump是获取数组或对象快照的非常有用的函数。

达令说

好吧,我知道有一个公认的答案但是…对于更特殊的情况,您也可以使用以下一个:array_map(function($n)&nbsp;{&nbsp;echo&nbsp;$n['filename'];&nbsp;echo&nbsp;$n['filepath'];},$array);或者以一种更不复杂的方式:function&nbsp;printItem($n){ &nbsp;&nbsp;&nbsp;&nbsp;echo&nbsp;$n['filename']; &nbsp;&nbsp;&nbsp;&nbsp;echo&nbsp;$n['filepath'];}array_map('printItem',&nbsp;$array);这将允许您以更轻松的方式操作数据。

三国纷争

启动简单,没有HTML:foreach($database&nbsp;as&nbsp;$file)&nbsp;{ &nbsp;&nbsp;&nbsp;&nbsp;echo&nbsp;$file['filename']&nbsp;.&nbsp;'&nbsp;at&nbsp;'&nbsp;.&nbsp;$file['filepath'];}否则你就可以操纵前额中的字段。
打开App,查看更多内容
随时随地看视频慕课网APP