Warning: file_get_contents(/data/phpspider/zhask/data//catemap/1/php/287.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/8/mysql/57.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
PHP:显示单数组数据?_Php_Mysql_Arrays - Fatal编程技术网

PHP:显示单数组数据?

PHP:显示单数组数据?,php,mysql,arrays,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] =>

所以代码是

$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']

但我犯了一个错误/

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

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

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

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

尝试此循环代码段:

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

您的
$gallery
是一个多维数组。您需要对其进行迭代。

尝试以下循环代码段:

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

您的
$gallery
是一个多维数组。您需要对其进行迭代。

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

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

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

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

$gallery变量现在是多维数组

你有

$gallery[0]['id'];
$gallery[1]['id'];
.....
现在,您可以使用foreach通过数组进行处理,也可以使用for循环进行处理

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

for($x=0;$x
您的$gallery变量现在是一个多维数组

你有

$gallery[0]['id'];
$gallery[1]['id'];
.....
现在,您可以使用foreach通过数组进行处理,也可以使用for循环进行处理

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

for($x=0;$x
您可以这样获得数据。不用绑在钥匙上


current($gallery)['id']
您可以这样获得数据。不用绑在钥匙上


current($gallery)['id']

foreach循环是显示它的唯一方法吗?如您所示,我尝试使用foreach循环,但它显示了表中的所有“id:”(foreach循环是显示它的唯一方法?我尝试如您所示使用foreach循环,但它显示了表中的所有“id:”(你的问题不在于你正在打印什么,而在于你的
get_gallery
函数返回什么——如果它只给你一个图像——你不需要返回其他图像的所有数据。你的问题不在于你正在打印什么,而在于你的
get_gallery
函数返回什么——如果它只给你一个图像——你不需要需要返回其他人的所有数据