php处理数组中的数组?

时间:2011-06-30 10:30:59

标签: php arrays

如何处理数组中的数组?

像这样:

Array
(
[page] => 1
[pages] => 391
[perpage] => 3
[total] => 1171
[photo] => Array
    (
        [0] => Array
            (
                [id] => 4539740346
                [owner] => 21229296@N03
                [secret] => 3b10921450
                [server] => 4040
                [farm] => 5
                [title] => ~ Berry One ~
                [ispublic] => 1
                [isfriend] => 0
                [isfamily] => 0
            )

编辑: 如何回应所有这些? 这是数组看起来像: http://www.qeeker.com/flickr

2 个答案:

答案 0 :(得分:2)

您可以从数组中获取值:

 echo $a['photo'][0]['ispublic']; //1

<强>更新

在您说的评论中,您想要显示照片内容。在PHP中,foreach可用于迭代数组:

foreach ($a['photo'] as $photo) {
    //$photo refers to the current photo on every iteration
    echo $photo['ispublic']; 
}

此代码段将回显阵列中每张照片的ispublic属性。如果要回显所有照片的所有属性,可以使用嵌套的foreach(一个在另一个内)。

答案 1 :(得分:0)

$var = array('page' => 1,
             'pages' => 391,
             'perpage' => 3,
             'total' => 1171,
             'photo' => array(array('id' => 4539740346,
                                    'owner' => '21229296@N03'
                                    'secret' => '3b10921450'
                                    'server' => 4040
                                    'farm' => 5
                                    'title' => '~ Berry One ~'
                                    'ispublic' => 1
                                    'isfriend' => 0
                                    'isfamily' => 0
                                    )
                               )
       );

var_dump($var);