TinyButStrong - the PHP Template Engine
Categories > TinyButStrong general >

Multidimensional arrays: lists inside lists

The forum is closed. Please use Stack Overflow for submitting new questions. Use tags: tinybutstrong , opentbs

By: Vincent
Date: 2015-04-17
Time: 08:09

Multidimensional arrays: lists inside lists

Hi,

I'm writing a middle layer to connect to an API. I'm collecting a lot of information into 1 array and then showing it using TBS and OpenTBS.
PHP:
$TBS->MergeBlock( 'document', $_SESSION['results'][$srchid] );
and the array:
Array
(
    [title] => Array
        (
            [en] => English title
            [fr] => French title
            [de] => German title
        )

    [dates] => Array
        (
            [event] => Array
                (
                    [date] => 24-02-2005
                    [name] => Priority
                )

            [anotherevent] => Array
                (
                    [date] => 24-02-2005
                    [name] => Filed
                )

        )

    [people] => Array
        (
            [0] => name 1
            [1] => name 2
            [2] => name 3
        )

Template:
<tr>
    <td>[document.dates.event.date;noerr]</td>
    <td>[document.title.en;noerr]</td>
    <td>[document.people.0;noerr]</td>
</tr>

In this template I'm only showing the first element in the sub-array 'dates' and 'people', but I don't know how I should show the list of people and the list of dates inside the main block. I'm sure I can find a way by merging several parts of the array or by restructuring the array, but I would lose other possibilities by doing so. I would like to have something like this:

<tr>
    <td>[document.dates.$.date;noerr]</td>
    <td>[document.title.en;noerr]</td>
    <td>[document.people.$;noerr]</td>
</tr>
Is this a case of subblocks?
Thx!
By: lauren
Date: 2015-04-20
Time: 10:14

Re: Multidimensional arrays: lists inside lists

well it looks to me as if you are trying to display a hierarchical data structure in a non-hierarchical way, which is not really possible without some data gymnastics first

if you cannot change the source data structure (or don't want to because it correctly models the data and is used that way elsewhere) then simply add a step in your code, between gathering the data and displaying the data, where you create the flat list of items for display purposes

bear in mind though that any 1 to many relationships (like the people, for example) will cause the 1 side to be repeated for each many side if displayed in a flat list

something i have done on an export of similar data to excel was to have a line item for each 1 side with a variable number of columns for each many side

hth
By: Vincent
Date: 2015-04-20
Time: 10:18

Re: Multidimensional arrays: lists inside lists

Thx for at least confirming that I did not overlook something obvious :)

I am able to change the data structure, and will probably go down that road...

Thx for your help!