gpt4 book ai didi

php - 在 PHP 中循环遍历这个数组的最佳方法是什么?

转载 作者:IT老高 更新时间:2023-10-28 11:56:39 25 4
gpt4 key购买 nike

我有这个数组...你如何打印每个文件路径和文件名?最好的方法是什么?

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

最佳答案

使用没有键的 foreach 循环:

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

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

使用带有键的 foreach 循环:

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

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

使用 for 循环:

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

var_dump 是一个非常有用的函数,可以获取数组或对象的快照。

关于php - 在 PHP 中循环遍历这个数组的最佳方法是什么?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/4414623/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com