Reputation: 18029
How can I print the filepath and filename values from each row?
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
)
)
Upvotes: 161
Views: 459930
Reputation: 47894
In modern PHP, array destructuring can be used to access only specific row columns. To print, string concatenation and interpolation can be avoided by using printf()
instead of echo
.
foreach ($array as ['filename' => $fn, 'filepath' => $fp]) {
printf("filename: %s, filepath: %s\n", $fn, $fp);
}
It is a misuse of a native function to call array_map()
and NOT use its return value. If functional-style iteration is desired, then iterate with array_walk()
because its return value is not designed to represent the iterated data.
array_walk($array, fn($row) =>
printf("filename: %s, filepath: %s\n", $row['filename'], $row['filepath'])
);
Upvotes: 0
Reputation: 29536
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.
Upvotes: 356
Reputation: 117
foreach($array as $item => $values) {
echo $values->filepath;
}
Upvotes: 6
Reputation: 350
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.
Upvotes: 14
Reputation: 1
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)))
{
...
}
Upvotes: -4
Reputation: 2074
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.
Upvotes: 6