Skip to main content

This site requires you to update your browser. Your browsing experience maybe affected by not having the most up to date version.

We've moved the forum!

Please use forum.silverstripe.org for any new questions (announcement).
The forum archive will stick around, but will be read only.

You can also use our Slack channel or StackOverflow to ask for help.
Check out our community overview for more options to contribute.

General Questions /

General questions about getting started with SilverStripe that don't fit in any of the categories above.

Moderators: martimiz, Sean, Ed, biapar, Willr, Ingo, swaiba

Accessing a multi-dimensional array on a template


Go to End


2 Posts   6142 Views

Avatar
Double-A-Ron

Community Member, 607 Posts

14 November 2013 at 5:37pm

I am pulling a source of data from an API, and using json_decode, converting it to an array. The idea is to push this data to a template and loop it to display rows in a table.

I found that unlike most other MVC frameworks, Silverstripe does not seem to have a native way of pushing an array to the template engine. I found many posts and snippets for doing this by manually converting arrays to ArrayLists and ArrayData objects. But no examples covered much past single dimension arrays.

This array is around 5 levels deep in some places. And some element keys are not known (dates/ids). In any case I managed to manually create a ArrayList object where each level from the array is represented by an ArrayData object.

Here is the code:

$rowTemp["cell1"] = $dayData['item']['one'];
$rowTemp["cell2"] = $dayData['item']['two'];
$rowTemp["cell3"] = $dayData['item']['three'];
$rowTemp["cell4"] = $dayData['item']['four'];
$rowTemp["cell5"] = $dayData['item']['five'];
$rowTemp["cell6"] = $dayData['item']['six'];
$rowTemp["cell7"] = $dayData['item']['seven'];
$rowTemp["cell8"] = $dayData['item']['eight'];
$rowTemp["cell9"] = $dayData['item']['nine'];
$rowTemp["cell10"] = $dayData['item']['ten'];

$finalRows[$datestamp] = new ArrayData($rowTemp); // Had to do this just to get the key "rows" in the ArrayData structure

// Did some stuff with totals and averages in the above fasion

$finalStructure = new ArrayData(array(
			new ArrayData($finalRows),
			new ArrayData($finalTotals),
			new ArrayData($finalAverages)		
		));

$mpqTableRows = new ArrayList(array($finalStructure));	

This is what I ended up with:

ArrayList Object
(
    [items:protected] => Array
        (
            [0] => ArrayData Object
                (
                    [array:protected] => Array
                        (
                            [rows] => ArrayData Object
                                (
                                    [array:protected] => Array
                                        (
                                            [1379073600000] => ArrayData Object
                                                (
                                                    [array:protected] => Array
                                                        (
                                                            [cell1] => 5618
                                                            [cell2] => 6840
                                                            [cell3] => 517.4
                                                            [cell4] => 632.1
                                                            [cell5] => 517.4
                                                            [cell6] => 632.1
                                                            [cell7] => 8.98
                                                            [cell8] => 8.98
                                                            [cell9] => 205.00
                                                            [cell10] => 204.00
                                                        )

Now I am calling this objected on my template using a method in the controller that just returns it. It seems to pull the object. But I cannot effectively loop through it to the point I can see rows.

Even when I break this object down and send just the "rows" element to the template, <% loop getRows %> returns just one item when there are many if I dump the same object. I assume this is due to the fact that the contents of that ArrayData object is one Array.

If all this is confusing, welcome to the last two days of work and loving memories of Symfony/Twig's {{ this.is.a.six.level.array }} simplicity.

The Question How do you get multi dimensional arrays from PHP accessible by the templates. Any examples online, even existing modules, would be great.

Avatar
Matt

Community Member, 86 Posts

14 February 2014 at 1:36pm

Here's one I wrote recently, which I want to add to the ArrayLib class, but haven't yet written tests for, so haven't sent a PR in yet.

Let me know what you think. It hasn't been extensively tested - just in one project of mine where I needed it to convert an array into something I could use in templates.

http://sspaste.com/paste/show/52fd61e09c10b