Referencing an index in an array in SMARTY template
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 :)
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()}
精彩评论