Forums / Setup & design / How to assign a value to a single array element?

How to assign a value to a single array element?

Author Message

E Hoelzler

Saturday 30 December 2006 3:21:04 am

The following code shows, how I tried to modify single elements of an array and the result.

{def $myarray=array(5,7,9)}
myarray = {$myarray[0]}, {$myarray[1]}, {$myarray[2]}<br>
or: {$myarray.0}, {$myarray.1}, {$myarray.2}<br>
loop display:<br>
{foreach $myarray as $ma}
  myarray-element: {$ma}<br>
{/foreach}
{set $myarray.0=20}  {* WHY DOESNT IT WORK?*}
now the array is destroyed<br>
myarray = {$myarray[0]}, {$myarray[1]}, {$myarray[2]}<br>
{foreach $myarray as $ma}
  myarray-element: {$ma}<br>
{/foreach}
{* result not better using an associative array*}
{def $myassocarray=hash('first', 'a',
			'second', 'b',
			'third', 'c')}

myassocarray = {$myassocarray.first}, {$myassocarray.second}, {$myassocarray.third}<br>
myassocarray(alt) = {$myassocarray[first]}, {$myassocarray[second]}, {$myassocarray[third]}<br>
{set $myassocarray.first='x'}

myassocarray(changed) = {$myassocarray.first}, {$myassocarray.second}, {$myassocarray.third}

The result was:

myarray = 5, 7, 9
or: 5, 7, 9
loop display:
myarray-element: 5
myarray-element: 7
myarray-element: 9

now the array is destroyed
myarray = , , 
myassocarray = a, b, c
myassocarray(alt) = a, b, c
myassocarray(changed) = , , 

Claudia Kosny

Tuesday 02 January 2007 2:11:58 pm

Hi E

As you most likely have found out by now assigning a new value to an array element is not possible directly. This fact is hidden quite well in the docu:

The "set" function can not be used to change the value of an element/attribute of an array, hash or an object. In fact, the elements/attributes of arrays, hashes and objects can not be changed from within template code.

http://ez.no/doc/ez_publish/technical_manual/3_8/templates/the_template_language/variable_usage

So best write yourself a custom template operator that does the job, it is really easy to do. First check the contribution section though, maybe there is already such an operator there.

Claudia

kracker (the)

Tuesday 02 January 2007 10:14:10 pm

interesting, it might take a person a long time to clearly see that end. that's worth documenting more clearly.

//kracker

Member since: 2001.07.13 || http://ezpedia.se7enx.com/

E Hoelzler

Wednesday 03 January 2007 12:04:04 pm

Hi Claudia, thank you for your hint - Indeed - I have read that part of the documentation several times. And I didn't recognize those tiny little sentences. And kracker is right: It took me quite a time to get to this point.
What I ACTUALLY wanted to do, I will realize now using a custom event.

Thanks for help

Erwin

Betsy Gamrat

Wednesday 24 January 2007 5:34:25 pm

http://ez.no/doc/ez_publish/technical_manual/3_8/reference/template_operators/arrays/insert

eZ debug

Timing: Jan 31 2025 06:28:05
Script start
Timing: Jan 31 2025 06:28:05
Module start 'content'
Timing: Jan 31 2025 06:28:05
Module end 'content'
Timing: Jan 31 2025 06:28:05
Script end

Main resources:

Total runtime0.8763 sec
Peak memory usage4,096.0000 KB
Database Queries204

Timing points:

CheckpointStart (sec)Duration (sec)Memory at start (KB)Memory used (KB)
Script start 0.00000.0071 588.0781180.8125
Module start 'content' 0.00710.7677 768.89061,036.1250
Module end 'content' 0.77480.1014 1,805.0156337.0547
Script end 0.8763  2,142.0703 

Time accumulators:

 Accumulator Duration (sec) Duration (%) Count Average (sec)
Ini load
Load cache0.00400.4582210.0002
Check MTime0.00140.1622210.0001
Mysql Total
Database connection0.00090.100210.0009
Mysqli_queries0.792290.40592040.0039
Looping result0.00170.19582020.0000
Template Total0.847796.720.4238
Template load0.00210.240320.0011
Template processing0.845696.491520.4228
Template load and register function0.00110.128010.0011
states
state_id_array0.00090.105810.0009
state_identifier_array0.00060.071220.0003
Override
Cache load0.00180.2025260.0001
Sytem overhead
Fetch class attribute can translate value0.00110.124350.0002
Fetch class attribute name0.00200.233470.0003
XML
Image XML parsing0.00290.332350.0006
class_abstraction
Instantiating content class attribute0.00000.001670.0000
General
dbfile0.00760.8646350.0002
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
5content/datatype/view/ezxmltext.tpl<No override>extension/community_design/design/suncana/templates/content/datatype/view/ezxmltext.tplEdit templateOverride template
7content/datatype/view/ezxmltags/paragraph.tpl<No override>extension/ezwebin/design/ezwebin/templates/content/datatype/view/ezxmltags/paragraph.tplEdit templateOverride template
3content/datatype/view/ezxmltags/literal.tpl<No override>extension/community/design/standard/templates/content/datatype/view/ezxmltags/literal.tplEdit templateOverride template
2content/datatype/view/ezimage.tpl<No override>extension/sevenx/design/simple/templates/content/datatype/view/ezimage.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: 20
 Number of unique templates used: 7

Time used to render debug report: 0.0001 secs