Forums / Setup & design / Best way in foreach

Best way in foreach

Author Message

Børge Warvik

Thursday 22 February 2007 2:17:55 am

Hi

What is the best way to get the last item in a foreach loop? I've been doing this with an index variable that increases every loop and check agains the array count.

Claudia Kosny

Thursday 22 February 2007 4:59:44 am

Hi Borge

This depends a bit on where you need the last item. If you just need the last item in an array (without doing the loop) the array operator extract_right might be what you are looking for.
If you loop anyway and need the last item outside of the loop, you could def a variable outside the loop and set it to the current item within the loop. I am not sure how good this is in regards to performance as I don't know what the template language is doing when setting an variable. In PHP this would be alright as it just uses references.
If you need to know the last item within the loop the way you are doing it sounds alright.If your array is numerically indexed you can use the $index in {foreach $array as $index => $value} as iteration counter so you don't need a separate index variable. Also be careful not to compute the count every time in the loop.

Claudia

eZ debug

Timing: Jan 18 2025 21:06:20
Script start
Timing: Jan 18 2025 21:06:20
Module start 'content'
Timing: Jan 18 2025 21:06:21
Module end 'content'
Timing: Jan 18 2025 21:06:21
Script end

Main resources:

Total runtime1.0438 sec
Peak memory usage4,096.0000 KB
Database Queries191

Timing points:

CheckpointStart (sec)Duration (sec)Memory at start (KB)Memory used (KB)
Script start 0.00000.0081 587.6875180.8438
Module start 'content' 0.00810.8655 768.5313496.8906
Module end 'content' 0.87360.1702 1,265.4219332.9297
Script end 1.0437  1,598.3516 

Time accumulators:

 Accumulator Duration (sec) Duration (%) Count Average (sec)
Ini load
Load cache0.00470.4548210.0002
Check MTime0.00140.1380210.0001
Mysql Total
Database connection0.00120.117810.0012
Mysqli_queries0.970792.99791910.0051
Looping result0.00260.24701890.0000
Template Total1.013997.120.5069
Template load0.00250.243420.0013
Template processing1.011396.887720.5057
Template load and register function0.00010.009710.0001
states
state_id_array0.00070.063910.0007
state_identifier_array0.00080.074920.0004
Override
Cache load0.00200.1904120.0002
Sytem overhead
Fetch class attribute can translate value0.00160.150230.0005
Fetch class attribute name0.00210.201630.0007
XML
Image XML parsing0.00090.083630.0003
class_abstraction
Instantiating content class attribute0.00000.000730.0000
General
dbfile0.00330.3122270.0001
String conversion0.00000.000830.0000
Note: percentages do not add up to 100% because some accumulators overlap

CSS/JS files loaded with "ezjscPacker" during request:

CacheTypePacklevelSourceFiles
CSS0extension/community/design/community/stylesheets/ext/jquery.autocomplete.css
extension/community_design/design/suncana/stylesheets/scrollbars.css
extension/community_design/design/suncana/stylesheets/tabs.css
extension/community_design/design/suncana/stylesheets/roadmap.css
extension/community_design/design/suncana/stylesheets/content.css
extension/community_design/design/suncana/stylesheets/star-rating.css
extension/community_design/design/suncana/stylesheets/syntax_and_custom_tags.css
extension/community_design/design/suncana/stylesheets/buttons.css
extension/community_design/design/suncana/stylesheets/tweetbox.css
extension/community_design/design/suncana/stylesheets/jquery.fancybox-1.3.4.css
extension/bcsmoothgallery/design/standard/stylesheets/magnific-popup.css
extension/sevenx/design/simple/stylesheets/star_rating.css
extension/sevenx/design/simple/stylesheets/libs/fontawesome/css/all.min.css
extension/sevenx/design/simple/stylesheets/main.v02.css
extension/sevenx/design/simple/stylesheets/main.v02.res.css
JS0extension/ezjscore/design/standard/lib/yui/3.17.2/build/yui/yui-min.js
extension/ezjscore/design/standard/javascript/jquery-3.7.0.min.js
extension/community_design/design/suncana/javascript/jquery.ui.core.min.js
extension/community_design/design/suncana/javascript/jquery.ui.widget.min.js
extension/community_design/design/suncana/javascript/jquery.easing.1.3.js
extension/community_design/design/suncana/javascript/jquery.ui.tabs.js
extension/community_design/design/suncana/javascript/jquery.hoverIntent.min.js
extension/community_design/design/suncana/javascript/jquery.popmenu.js
extension/community_design/design/suncana/javascript/jScrollPane.js
extension/community_design/design/suncana/javascript/jquery.mousewheel.js
extension/community_design/design/suncana/javascript/jquery.cycle.all.js
extension/sevenx/design/simple/javascript/jquery.scrollTo.js
extension/community_design/design/suncana/javascript/jquery.cookie.js
extension/community_design/design/suncana/javascript/ezstarrating_jquery.js
extension/community_design/design/suncana/javascript/jquery.initboxes.js
extension/community_design/design/suncana/javascript/app.js
extension/community_design/design/suncana/javascript/twitterwidget.js
extension/community_design/design/suncana/javascript/community.js
extension/community_design/design/suncana/javascript/roadmap.js
extension/community_design/design/suncana/javascript/ez.js
extension/community_design/design/suncana/javascript/ezshareevents.js
extension/sevenx/design/simple/javascript/main.js

Templates used to render the page:

UsageRequested templateTemplateTemplate loadedEditOverride
1node/view/full.tplfull/forum_topic.tplextension/sevenx/design/simple/override/templates/full/forum_topic.tplEdit templateOverride template
1content/datatype/view/ezimage.tpl<No override>extension/sevenx/design/simple/templates/content/datatype/view/ezimage.tplEdit templateOverride template
2content/datatype/view/ezxmltext.tpl<No override>extension/community_design/design/suncana/templates/content/datatype/view/ezxmltext.tplEdit templateOverride template
3content/datatype/view/ezxmltags/paragraph.tpl<No override>extension/ezwebin/design/ezwebin/templates/content/datatype/view/ezxmltags/paragraph.tplEdit templateOverride template
1content/datatype/view/ezxmltags/line.tpl<No override>design/standard/templates/content/datatype/view/ezxmltags/line.tplEdit templateOverride template
1pagelayout.tpl<No override>extension/sevenx/design/simple/templates/pagelayout.tplEdit templateOverride template
 Number of times templates used: 9
 Number of unique templates used: 6

Time used to render debug report: 0.0002 secs