Php 使用smarty的多维阵列
我正在从事一个PHP项目,并试图将多维数组导出到Smarty 下面是我的阵列的填充方式:Php 使用smarty的多维阵列,php,arrays,smarty,Php,Arrays,Smarty,我正在从事一个PHP项目,并试图将多维数组导出到Smarty 下面是我的阵列的填充方式: $array = array(); while ($myrow = mysql_fetch_array($result)) { $array[$myrow['BugID']][$myrow['id']]['BugID'] = $myrow['BugID']; $array[$myrow['BugI
$array = array();
while ($myrow = mysql_fetch_array($result))
{
$array[$myrow['BugID']][$myrow['id']]['BugID'] = $myrow['BugID'];
$array[$myrow['BugID']][$myrow['id']]['DateSent'] = $myrow['DateSent'];
$array[$myrow['BugID']][$myrow['id']]['FromAddress'] = $encryption->decrypt($myrow['FromAddress']);
$array[$myrow['BugID']][$myrow['id']]['ToAddress'] = $encryption->decrypt($myrow['ToAddresss']);
$array[$myrow['BugID']][$myrow['id']]['Message'] = $encryption->decrypt($myrow['Message']);
}
$smarty->assign("array", $array);
$content = $smarty->fetch("message-list.tpl");
下面是我的模板文件
<table class="tableDetails">
<tr>
<th>Date Sent</th>
<th>Bug ID</th>
<th>From Address</th>
<th>To Address</th>
<th>Message</th>
</tr>
{foreach from=$array key=header item=table}
<tr>
<td>{$array.DateSent}</td>
<td>{$array.DateSent}</td>
<td>{$array.DateSent}</td>
<td>{$array.DateSent}</td>
<td>{$array.DateSent}</td>
</tr>
{/foreach}
</table>
Array
(
[2] => Array
(
[3] => Array
(
[BugID] => 2
[DateSent] => 2014-03-17 22:51:08
[FromAddress] => someone@example.com
[ToAddress] =>
[Message] => This is a test.
)
)
[1] => Array
(
[2] => Array
(
[BugID] => 1
[DateSent] => 2014-03-15 00:04:03
[FromAddress] => someone@example.com
[ToAddress] =>
[Message] => This is a message from the bug administrator to the bug reporter
)
[1] => Array
(
[BugID] => 1
[DateSent] => 2014-03-15 00:03:17
[FromAddress] => someone@example.com
[ToAddress] =>
[Message] => This is the content of the message from the bug reporter to the administrator
)
)
)
发送日期
错误ID
发件人地址
解决
消息
{foreach from=$array key=header item=table}
{$array.DateSent}
{$array.DateSent}
{$array.DateSent}
{$array.DateSent}
{$array.DateSent}
{/foreach}
排列
(
[2] =>阵列
(
[3] =>阵列
(
[BugID]=>2
[发送日期]=>2014-03-17 22:51:08
[FromAddress]=>someone@example.com
[地址]=>
[消息]=>这是一个测试。
)
)
[1] =>阵列
(
[2] =>阵列
(
[BugID]=>1
[发送日期]=>2014-03-15 00:04:03
[FromAddress]=>someone@example.com
[地址]=>
[消息]=>这是错误管理员向错误报告者发送的消息
)
[1] =>阵列
(
[BugID]=>1
[发送日期]=>2014-03-15 00:03:17
[FromAddress]=>someone@example.com
[地址]=>
[消息]=>这是错误报告器发送给管理员的消息内容
)
)
)
目前,我的表中没有输出任何内容,除了表头,实际数组中没有输出任何内容
谢谢你能提供的帮助 更新答案 您需要迭代每个“bug”的内部数组。见:
不幸的是,这对我没有帮助。这样就可以将所有不同的ID分配给一个具有bugID值索引的数组,这样做会改变结构,无法满足我的需要请阅读smarty foreach的工作原理:
<?php
// index.php
require_once('smarty/Smarty.class.php');
$smarty = new Smarty();
$bugs = array();
$bugs[1][412]['BugID'] = 1;
$bugs[1][412]['DateSent'] = date("F jS, Y");
$bugs[1][412]['FromAddress'] = "foo@bar.com";
$bugs[1][412]['ToAddress'] = "bar@foo.com";
$bugs[1][412]['Message'] = "this is a test!";
$bugs[2][534]['BugID'] = 2;
$bugs[2][534]['DateSent'] = date("F jS, Y");
$bugs[2][534]['FromAddress'] = "test@test.com";
$bugs[2][534]['ToAddress'] = "tosh@tosh.com";
$bugs[2][534]['Message'] = "lorem ipsum!";
$smarty->assign('bugs', $bugs);
$smarty->display("index.tpl");
<table class="tableDetails">
<tr>
<th>Date Sent</th>
<th>Bug ID</th>
<th>From Address</th>
<th>To Address</th>
<th>Message</th>
</tr>
{foreach from=$bugs key=parentID item=table}
{foreach from=$table key=bugUID item=bug}
<tr>
<td>{$bug.DateSent}</td>
<td>{$parentID}</td>
<td>{$bug.FromAddress}</td>
<td>{$bug.ToAddress}</td>
<td>{$bug.Message}</td>
</tr>
{/foreach}
{/foreach}
</table>