How to print a value from mutiple arrays in PHP?
e.g. Here is the structure I have and I want to print filepath
from these arryas, here it's clear that we have two arrays but in my case it's dynamic. How can I print filepath
from unknown number of arrays?
[field_pic] => Array
(
[0] => Array
(
[fid] => 29
[uid] => 1
[filename] => 011.jpg
[filepath] => sites/default/files/011.jpg
[filemime] => image/jpeg
[filesize] => 154608
[status] => 1
[timestamp] => 1349463183
[list] => 1
[data] =>
)
[1] => Array
(
[fid] => 31
[uid] => 1
[filename] => 019.jpg
[filepath] => sites/default/files/019.jpg
[filemime] => image/jpeg
[filesize] => 158635
[status] => 1
[timestamp] => 1349463188
[list] => 1
[data] =>
)
)
try:
for($i = count($field_pic) -1 ; $i >= 0 ; $i-- ){ //You have to substract 1 to the array length because it's of bound
echo "filepath=" . $field_pic[$i]['filepath'];
}
Example :
$arr = array(1,2,3)
Count($arr) = 3;
but `$array[3] = undefined;
foreach($field_pic as $pic)
var_dump($pic["filepath"]);
do you want it like this?
Outputs each file path on it's own line
foreach( $field_pics as $field_pic )
{
echo $field_pic['filepath'] . '<br />';
}
Assume your 2D array is called $main
the following should achieve your goal:
#create an array to store the filenames
$filenames_array = array();
# this will loop through the first array
foreach($main as $subarray)
{
# append the filename to the end of the array you created to store these names
$filenames_array[] = $subarray['filename'];
}
var_dump($filenames_array);
That should solve this problem.
P.S. Is this your homework?
Really Thanks to everyone, This is how I fixed it:
for($i = count($node->field_pic) ; $i >= 0 ; $i-- ){
echo $node->field_pic[$i]['filepath'];
}