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

发布于 2024-11-17 19:14:57 字数 4086 浏览 3 评论 0原文

我有一个 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] 但没有成功。

非常感谢任何帮助:)

I have a variable that is an array in Smarty, and I am trying to figure out how to retrieve that information. The variable is called $COMMENTS and I did $COMMENTS|@print_r and this is what I got:

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
        ) 
    )
)

I am trying to retrieve the 11686 number from that. Any help? I have tried $COMMENTS[0][data:private][0] but that didn't work.

Any help greatly appreciated :)

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

作死小能手 2024-11-24 19:14:57

首先,将 print_r() 输出放入

 标记中,以便其可读。

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()}

First, put print_r() output into <pre> tags so it is readable.

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

You're trying to access the first object in your array, $COMMENTS[0]. Since that it is object, and your data property is private, you cannot access it in Smarty. You would have to edit the ModComments_CommentsModel class to give you access to either the data property or the crmid key of the property.

Example:

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()}
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文