循环遍历数组 php
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/4414623/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Loop through an array php
提问by esafwan
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
)
)
回答by Ish
Using foreach
loop without key
使用foreach
无键循环
foreach($array as $item) {
echo $item['filename'];
echo $item['filepath'];
// to know what's in $item
echo '<pre>'; var_dump($item);
}
Using foreach
loop with key
使用foreach
带键的循环
foreach($array as $i => $item) {
echo $item[$i]['filename'];
echo $item[$i]['filepath'];
// $array[$i] is same as $item
}
Using for
loop
使用for
循环
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.
var_dump
是一个非常有用的函数,用于获取数组或对象的快照。
回答by obsergiu
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.
这将允许您以更简单的方式操作数据。
回答by SilverbackNet
Starting simple, with no HTML:
从简单开始,没有 HTML:
foreach($database as $file) {
echo $file['filename'] . ' at ' . $file['filepath'];
}
And you can otherwise manipulate the fields in the foreach.
并且您可以以其他方式操作 foreach 中的字段。
回答by Kavinda Harshana
foreach($array as $item=>$values){
echo $values->filepath;
}
回答by jakoubekcz
You can use also this without creating additional variables nor copying the data in the memory like foreach() does.
您也可以使用它,而无需创建额外的变量,也无需像 foreach() 那样复制内存中的数据。
while (false !== (list($item, $values) = each($array)))
{
...
}