在foreach循环中多次显示最后一个元素

时间:2012-12-06 12:45:36

标签: php arrays loops foreach

那边是Hy。 我有一个有7个房间的单元,但其中只有5个有图片。我的目标是在正确的位置展示它们,即1#room-> 1img,2#room-> 1img,3#room-> 3img,4#room-> 2img,5#room- > 1img,6#room-> 0img,7#room-> 0img。这是我的阵列:

Array
(
    [0] => Array
        (
            [large] =>room_galleries/4/large/XWKgkS_a503fd0054f4c6a04e72d95cef5d8b6a.jpg
            [thumb] =>room_galleries/4/thumb/XWKgkS_a503fd0054f4c6a04e72d95cef5d8b6a.jpg
        )

)
Array
(
    [0] => Array
        (
            [large] => room_galleries/13/large/96Hk2r_6e2507fabb7313d18d9a694eda740337.jpg
            [thumb] => room_galleries/13/thumb/96Hk2r_6e2507fabb7313d18d9a694eda740337.jpg
        )

)
Array
(
    [0] => Array
        (
            [large] => room_galleries/38/large/HXJTWL_98e71e7fbc9bab7096f0387b2ffa9ab9.jpg
            [thumb] => room_galleries/38/thumb/HXJTWL_98e71e7fbc9bab7096f0387b2ffa9ab9.jpg
        )

    [1] => Array
        (
            [large] =>room_galleries/38/large/XCquAP_5ccd9c6af4501a1c81b1e6b532100036.jpg
            [thumb] =>room_galleries/38/thumb/XCquAP_5ccd9c6af4501a1c81b1e6b532100036.jpg
        )

    [2] => Array
        (
            [large] => room_galleries/38/large/iDe0SN_6dcf2e3c9dc2180cbed5c98f0adbb0e9.jpg
            [thumb] => room_galleries/38/thumb/iDe0SN_6dcf2e3c9dc2180cbed5c98f0adbb0e9.jpg
        )

)
Array
(
    [0] => Array
        (
            [large] => room_galleries/39/large/FDM6JV_63243ea8328f4ba74200ea3c2d32aa0c.jpg
            [thumb] => room_galleries/39/thumb/FDM6JV_63243ea8328f4ba74200ea3c2d32aa0c.jpg
        )

    [1] => Array
        (
            [large] => room_galleries/39/large/ccNOdB_a12501222468f3e38f05433c002b8851.jpg
            [thumb] => room_galleries/39/thumb/ccNOdB_a12501222468f3e38f05433c002b8851.jpg
        )

)
Array
(
    [0] => Array
        (
            [large] => room_galleries/41/large/IosSy8_e4d6ff02cc21212c6cf6e616619f11b6.jpg
            [thumb] => room_galleries/41/thumb/IosSy8_e4d6ff02cc21212c6cf6e616619f11b6.jpg
        )

)

这是我的php代码:

if( !empty( $r['images'])){
            $images = unserialize( $r['images'] );
            foreach( $images as $img ){
                $galleries[$r['roomID']][] = array(
                    'large' => ROOM_GALLERY_HTTP . $r['roomID'] . '/large/' . $img,
                    'thumb' =>  ROOM_GALLERY_HTTP . $r['roomID'] . '/thumb/' . $img,
                );  
            }
            print_r( $galleries[$r['roomID']] ); // the array above
            $tpl->set( 'galleries', $galleries[$r['roomID']] ); // set a tag named galleries for my template
        }

我的问题是它在循环中显示最后一个元素7次:

Array
(
    [0] => Array
        (
            [large] => room_galleries/41/large/IosSy8_e4d6ff02cc21212c6cf6e616619f11b6.jpg
            [thumb] => room_galleries/41/thumb/IosSy8_e4d6ff02cc21212c6cf6e616619f11b6.jpg
        )

)

有什么建议吗?

0 个答案:

没有答案