Announcement
Collapse
No announcement yet.
rendering contents of multi-dimensional array structure
Collapse
X
-
You can use miva_array_collapse in place of miva_array_max to rewrite non-sequential indices before returning the number of elements.
-
For the outer array, one call to miva_array max() will give you the number of elements it contains. But miva_array_elements() is a better choice if there's any chance that the outer array is not indexed by consecutive numbers (i.e. a sparse array).
For the inner arrays, you'll need to use a loop that calls miva_array_max (or miva_array elements) on each one, and save the largest value, something like:Code:<MvFOREACH ARRAY="l.outer" ITERATOR="l.inner"> <MvASSIGN NAME="l.innercount" VALUE="{ miva_array_elements(l.inner) }"> <MvIF EXPR="{ l.innercount GT l.maxcount }"> <MvASSIGN NAME="l.maxcount" VALUE="{ l.innercount }"> </MvIF> </MvFOREACH>
I don't know of a built-in function, documented or otherwise, that will do this
Leave a comment:
-
There were simply illustrating the need to get the max elements for the outer array and the max element for each of the inners. IOW if they were rows and columns row 1, 4 row 2, 3, row 3, 1, etc.
So yes, each of the inner arrays. Adding to my fun, each of the inners potentially have members and arrays.
This isn't a perfect example but does depict the index scenario:
Code:set[1][1]:attr:options[1]:cost=0.01 set[1][1]:attr:options[1]:type=A set[1][2]:attr:options[1]:cost=0.02 set[1][2]:attr:options[1]:type=B set[2][1]:attr:options[1]:cost=0.03 set[2][1]:attr:options[1]:type=C set[2][1]:attr:options[2]:cost=0.04 set[2][1]:attr:options[2]:type=D
Scott
Leave a comment:
-
I guess I don't understand your pseudo-code. Are you trying to look at an array of arrays, and find the largest number of elements for any of the inner arrays?
Leave a comment:
-
There was a reason I asked the question that way. The client has many pages of template code that use MVT:While to loop through this array structure. It's more intense than I've illustrated. Moving to Foreach might be unavoidable but it isn't what the client wants to see. The miva_array_max statements were pseudo code. Apologies for not being specific, but since the variables could never work I thought the illustration was enough. I guess I was hoping for an undocumented function that could spit out the stats.
Scott
Leave a comment:
-
I've written some modules that work with multi-dimensional arrays, e.g for managing variants for products that have multiple attributes. But I must admit that I don't understand what you're asking for here. Maybe I'm just slow today, or maybe you need to fill in some details.
Are the ":element" members arrays? If not, why are you using miva_array_max on them?
Can you use MvFOREACH instead of MvWHILE? Then you don't have to know in advance how many elements an array has.
Leave a comment:
-
rendering contents of multi-dimensional array structure
I wasn't planning this, and it turns out to be easy to create this enigma. I've created the array structure in a custom module -- it solved a complex dataset. Now I need to render in a page template. I'm using SMT code here. but I may need to do it in Mivascript too. The issue is I don't know the "max" values so I can stop the loop.
This illustrates what I need:
Code:<mvt:assign name="g.totalouter" value="miva_array_max(g.data:set[l.settings:outercounter][l.settings:innercounter]:element) " /> <mvt:assign name="g.totalinner" value="miva_array_max(g.data:set[l.settings:outercounter][l.settings:innercounter]:element) " />
Code:<mvt:assign name="l.settings:innercounter" value="1" /> <mvt:assign name="l.settings:outercounter" value="1" /> <mvt:comment> find the value of g.totalouter </mvt:comment> <mvt:while expr="l.settings:outercounter LE g.totalouter"> <mvt:comment> find the value of g.totalinner </mvt:comment> <mvt:while expr="l.settings:innercounter LE g.totalinner"> <mvt:eval expr = "g.data:set[l.settings:outercounter][l.settings:innercounter]:element" /> <mvt:assign name="l.settings:innercounter" value="l.settings:innercounter + 1" /> </mvt:while> <mvt:assign name="l.settings:outercounter" value="l.settings:outercounter + 1" /> </mvt:while>
There is a way to accomplish this, right? Might be simple, but I don't see it.
Thanks,
ScottTags: None
Leave a comment: