Search code examples
phptemplatessmartyprestashop

access nested associative array in smarty template


I have a php array named consolidatedReviewData, which looks like this:

Array
(
    [consolidatedData] => Array
        (
            [0] => Array
                (
                    [a_productidentification] => 5920
                    [avg_rating] => 4.0000
                    [no_reviews] => 2
                )

        )

)

and I am assigning it to a smarty variable like this:

self::$smarty->assign('cosolidatedReviews', $consolidatedReviewData);

Now, my question is how do I directly access avg_rating inside the .tpl?

Something like:

{$consolidatedReviewData->consolidatedData->0->avg_rating}

Solution

  • Use dots to access array items:

    {$consolidatedReviews.consolidatedData.0.avg_rating}
    

    This is the same as the following in PHP:

    echo $consolidatedReviews['consolidatedData'][0]['avg_rating'];
    

    -> is for accessing object properties.

    See http://www.smarty.net/docs/en/language.syntax.variables.tpl