1

如何从 PHP 中的多个数组中打印一个值?

例如,这是我拥有的结构,我想filepath从这些数组中打印出来,很明显我们有两个数组,但在我的情况下它是动态的。如何filepath从未知数量的数组中打印?

[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] => 
            )
    )
4

5 回答 5

2

在其自己的行上输出每个文件路径

foreach( $field_pics as $field_pic )
{
    echo $field_pic['filepath'] . '<br />';
}
于 2012-10-05T19:47:30.907 回答
1
foreach($field_pic as $pic)
   var_dump($pic["filepath"]);

你想要这样吗?

于 2012-10-05T19:47:10.303 回答
1

假设您的二维数组被称为$main以下应该实现您的目标:

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

那应该可以解决这个问题。

PS这是你的作业吗?

于 2012-10-05T19:51:16.380 回答
0

尝试:

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'];
} 

例子 :

$arr = array(1,2,3) 

Count($arr) = 3; 但是`$array[3] = undefined;

于 2012-10-05T19:47:11.997 回答
0

真的感谢大家,这就是我修复它的方法:

for($i = count($node->field_pic) ; $i >= 0 ; $i-- ){
    echo $node->field_pic[$i]['filepath'];
} 
于 2012-10-05T21:11:18.043 回答