在SMARTY模板中引用数组中的索引

时间:2011-06-29 21:05:13

标签: php arrays reference indexing smarty

我有一个变量,它是Smarty中的一个数组,我试图找出如何检索该信息。该变量名为$ COMMENTS,我做了$COMMENTS|@print_r,这就是我得到的:

Array ( 
    [0] => ModComments_CommentsModel Object ( 
        [data:private] => Array ( 
            [0] => 11686 
            [crmid] => 11686 
            [1] => 1679 
            [smcreatorid] => 1679 
            [2] => 1679 
            [smownerid] => 1679 
            [3] => 0 
            [modifiedby] => 0 
            [4] => ModComments 
            [setype] => ModComments 
            [5] => 
            [description] => 
            [6] => 2011-06-08 15:00:31 
            [createdtime] => 2011-06-08 15:00:31 
            [7] => 2011-06-08 15:00:31 
            [modifiedtime] => 2011-06-08 15:00:31 
            [8] => 2011-06-29 12:00:23 
            [viewedtime] => 2011-06-29 12:00:23 
            [9] => 
            [status] => 
            [10] => 0 
            [version] => 0 
            [11] => 1 
            [presence] => 1 
            [12] => 0 
            [deleted] => 0 
            [13] => 11686 
            [modcommentsid] => 11686 
            [14] => aasd 
            [commentcontent] => aasd 
            [15] => 6730 
            [related_to] => 6730 
            [16] => 
            [parent_comments] => 
            [17] => 11686 
        )
    )
    [1] => ModComments_CommentsModel Object ( 
        [data:private] => Array ( 
            [0] => 11685 
            [crmid] => 11685 
            [1] => 1679 
            [smcreatorid] => 1679 
            [2] => 1679 
            [smownerid] => 1679 
            [3] => 0 
            [modifiedby] => 0 
            [4] => ModComments 
            [setype] => ModComments 
            [5] => 
            [description] => 
            [6] => 2011-06-08 14:58:42 
            [createdtime] => 2011-06-08 14:58:42 
            [7] => 2011-06-08 14:58:42 
            [modifiedtime] => 2011-06-08 14:58:42 
            [8] => 
            [viewedtime] => 
            [9] => 
            [status] => 
            [10] => 0 
            [version] => 0 
            [11] => 1 
            [presence] => 1 
            [12] => 0 
            [deleted] => 0 
            [13] => 11685 
            [modcommentsid] => 11685 
            [14] => comment 
            [commentcontent] => comment 
            [15] => 6730 
            [related_to] => 6730 
            [16] => 
            [parent_comments] => 
            [17] => 11685
        )
    )
    [2] => ModComments_CommentsModel Object ( 
        [data:private] => Array ( 
            [0] => 6731 
            [crmid] => 6731 
            [1] => 1679 
            [smcreatorid] => 1679 
            [2] => 1679 
            [smownerid] => 1679 
            [3] => 0 
            [modifiedby] => 0 
            [4] => ModComments 
            [setype] => ModComments 
            [5] => 
            [description] => 
            [6] => 2010-11-02 10:15:06 
            [createdtime] => 2010-11-02 10:15:06 
            [7] => 2010-11-02 10:15:06 
            [modifiedtime] => 2010-11-02 10:15:06 
            [8] => 
            [viewedtime] => 
            [9] => 
            [status] => 
            [10] => 0 
            [version] => 0 
            [11] => 1 
            [presence] => 1 
            [12] => 0 
            [deleted] => 0 
            [13] => 6731 
            [modcommentsid] => 6731 
            [14] => Test comment 
            [commentcontent] => Test comment 
            [15] => 6730 
            [related_to] => 6730 
            [16] => 
            [parent_comments] => 
            [17] => 6731
        ) 
    )
)

我正在尝试从中检索 11686 号码。有帮助吗?我尝试了$COMMENTS[0][data:private][0]但是没有用。

任何帮助都非常感谢:)

1 个答案:

答案 0 :(得分:0)

首先,将print_r()输出放入<pre>标记,以便它可读。

Array ( 
    [0] => ModComments_CommentsModel Object ( 
        [data:private] => Array ( 
            [0] => 11686 
            [crmid] => 11686 
            [1] => 1679 
            [smcreatorid] => 1679 
            [2] => 1679 
            ...
        )
    )
    ...
)

您正在尝试访问数组中的第一个对象$COMMENTS[0]。由于它是对象,并且您的data属性是私有的,因此您无法在Smarty中访问它。您必须编辑ModComments_CommentsModel类,才能访问属性的data属性或crmid键。

示例:

class ModComments_CommentsModel {

    // ...

    public function CrmId(){
        return $this->data['crmid'];
    }

    // ...

}

{* template *}
{$COMMENTS[0]->CrmId()}

{* might have to assign before using method call *}
{assign var='comment' value=$COMMENTS[0]}
{$comment->CrmId()}