Note that there are some explanatory texts on larger screens.

plurals
  1. POdisplaying last element in a foreach loop several times
    text
    copied!<p>Hy there. I have a unit with 7 room, but only 5 of them have pictures in it. My goal is to display each of them in the right place, ie 1#room->1img, 2#room->1img, 3#room->3img, 4#room->2img, 5#room->1img, 6#room->0img, 7#room->0img. Here is my array:</p> <pre><code>Array ( [0] =&gt; Array ( [large] =&gt;room_galleries/4/large/XWKgkS_a503fd0054f4c6a04e72d95cef5d8b6a.jpg [thumb] =&gt;room_galleries/4/thumb/XWKgkS_a503fd0054f4c6a04e72d95cef5d8b6a.jpg ) ) Array ( [0] =&gt; Array ( [large] =&gt; room_galleries/13/large/96Hk2r_6e2507fabb7313d18d9a694eda740337.jpg [thumb] =&gt; room_galleries/13/thumb/96Hk2r_6e2507fabb7313d18d9a694eda740337.jpg ) ) Array ( [0] =&gt; Array ( [large] =&gt; room_galleries/38/large/HXJTWL_98e71e7fbc9bab7096f0387b2ffa9ab9.jpg [thumb] =&gt; room_galleries/38/thumb/HXJTWL_98e71e7fbc9bab7096f0387b2ffa9ab9.jpg ) [1] =&gt; Array ( [large] =&gt;room_galleries/38/large/XCquAP_5ccd9c6af4501a1c81b1e6b532100036.jpg [thumb] =&gt;room_galleries/38/thumb/XCquAP_5ccd9c6af4501a1c81b1e6b532100036.jpg ) [2] =&gt; Array ( [large] =&gt; room_galleries/38/large/iDe0SN_6dcf2e3c9dc2180cbed5c98f0adbb0e9.jpg [thumb] =&gt; room_galleries/38/thumb/iDe0SN_6dcf2e3c9dc2180cbed5c98f0adbb0e9.jpg ) ) Array ( [0] =&gt; Array ( [large] =&gt; room_galleries/39/large/FDM6JV_63243ea8328f4ba74200ea3c2d32aa0c.jpg [thumb] =&gt; room_galleries/39/thumb/FDM6JV_63243ea8328f4ba74200ea3c2d32aa0c.jpg ) [1] =&gt; Array ( [large] =&gt; room_galleries/39/large/ccNOdB_a12501222468f3e38f05433c002b8851.jpg [thumb] =&gt; room_galleries/39/thumb/ccNOdB_a12501222468f3e38f05433c002b8851.jpg ) ) Array ( [0] =&gt; Array ( [large] =&gt; room_galleries/41/large/IosSy8_e4d6ff02cc21212c6cf6e616619f11b6.jpg [thumb] =&gt; room_galleries/41/thumb/IosSy8_e4d6ff02cc21212c6cf6e616619f11b6.jpg ) ) </code></pre> <p>Here is my php code:</p> <pre><code>if( !empty( $r['images'])){ $images = unserialize( $r['images'] ); foreach( $images as $img ){ $galleries[$r['roomID']][] = array( 'large' =&gt; ROOM_GALLERY_HTTP . $r['roomID'] . '/large/' . $img, 'thumb' =&gt; ROOM_GALLERY_HTTP . $r['roomID'] . '/thumb/' . $img, ); } print_r( $galleries[$r['roomID']] ); // the array above $tpl-&gt;set( 'galleries', $galleries[$r['roomID']] ); // set a tag named galleries for my template } </code></pre> <p>My problem is that it displays the last element in the loop 7 times.: </p> <pre><code>Array ( [0] =&gt; Array ( [large] =&gt; room_galleries/41/large/IosSy8_e4d6ff02cc21212c6cf6e616619f11b6.jpg [thumb] =&gt; room_galleries/41/thumb/IosSy8_e4d6ff02cc21212c6cf6e616619f11b6.jpg ) ) </code></pre> <p>Any suggestions?</p>
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload