PHP:显示单个数组数据?

时间:2013-04-27 13:26:07

标签: php mysql arrays

所以代码是

$gallery = get_gallery('gallery_id');
print_r ($gallery);

我得到了:

Array ( [0] => Array ( [id] => 13 [image] => 0 [user] => 13 [timestamp] => 1366237591 [ext] => png [caption] => Happy smile shi ma? [comment] => ) [1] => Array ( [id] => 14 [image] => 0 [user] => 13 [timestamp] => 1366237954 [ext] => jpg [caption] => Confused [comment] => ) [2] => Array ( [id] => 15 [image] => 0 [user] => 13 [timestamp] => 1366237979 [ext] => jpg [caption] => Facebookerg [comment] => ) [3] => Array ( [id] => 16 [image] => 0 [user] => 13 [timestamp] => 1366377510 [ext] => gif [caption] => lolwut? [comment] => ) [4] => Array ( [id] => 17 [image] => 0 [user] => 13 [timestamp] => 1366380899 [ext] => jpg [caption] => rorwut? [comment] => ) [5] => Array ( [id] => 18 [image] => 0 [user] => 13 [timestamp] => 1366651685 [ext] => jpg [caption] => Notes? [comment] => ) [6] => Array ( [id] => 19 [image] => 0 [user] => 13 [timestamp] => 1366711880 [ext] => jpg [caption] => asd [comment] => ) [7] => Array ( [id] => 20 [image] => 0 [user] => 14 [timestamp] => 1366940983 [ext] => jpg [caption] => Belzelga [comment] => ) )

哪个好,它终于奏效了。但是如何显示单个数据/表。 因为我试图从这些东西中获得一个'id'。 我试着回应$gallery['id'] 但是我收到了一个错误。 :/

5 个答案:

答案 0 :(得分:2)

您需要先访问正确的索引:

$gallery[0]['id']
//      ^^^

答案 1 :(得分:1)

你的$ gallery变量现在是一个多维数组。

你有

$gallery[0]['id'];
$gallery[1]['id'];
.....

现在你可以使用foreach来处理数组或for循环

foreach ($gallery as $anItem ) {
    echo $anItem['id'];
}

OR

for ( $x=0; $x < count($gallery); $x++ ) {
    echo $gallery[$x]['id'];
}

答案 2 :(得分:0)

试试这个循环代码段:

foreach( $gallery as $temp ) {
    echo $temp['id'];
}

您的$gallery是一个多维数组。你需要迭代它。

答案 3 :(得分:0)

如果您只想显示特定的图库,则应该使用类似www.yoursite.com/gallery.php?id=1的内容。然后在你的代码中显示它

    if(isset($_GET['id']))
    {
         foreach( $gallery as $g ) 
         {
           if($g['id'] == $_GET['id'])
                 echo $g['id'];
         }
    }

答案 4 :(得分:0)

您可以获取数据。不受钥匙束缚。

current($gallery)['id']