使用 in_array 和 smarty 部分循环

using in_array with smarty section loop

这是我的 smarty 模板文件:

{section loop=$Author name=Author}
<option value="{$Author[Author].Id}" 
{if $Author[Author].Id|in_array:$ListAths.AuthorId})}
selected
{/if}
>
{$Author[Author].AuthorName}</option>
{/section}

我的 PHP 两个变量(Author 和 ListAths)的结构看起来像这样用正确的变量替换 sql 和聪明的命令:

$stmt = $connect->prepare(/*command here*/);
$stmt->execute($array);
$result = $stmt->fetchall();
$smarty->assign('ListAths', $result);
$stmt2 = $connect->prepare(/*command here*/);
$stmt2->execute($array);
$result2 = $stmt2->fetchall();
$smarty->assign('Author', $result2);

我遇到的错误:

Warning: in_array() expects parameter 2 to be array, null given in /mydirectory/Smarty/sysplugins/smarty_internal_templatebase.php(171) : eval()'d code on line 71

$ListAths.AuthorId 没有被识别为数组!我该如何解决这个问题?

它是这样工作的:

{section loop=$Author name=Author}
<option value="{$Author[Author].Id}" 
{section loop=$ListAths name=ListAths}
{if $Author[Author].Id == $ListAths[ListAths].AuthorId}
selected
{/if}
{/section}
>
{$Author[Author].AuthorName}</option>
{/section}

但我还是想用 in_array

我无法评论询问您使用哪个 smarty 版本...假设 smarty 3 您可以在您的 smarty 模板中使用任何 PHP 函数(以及对象)...

{section loop=$Author name=Author}
<option value="{$Author[Author].Id}" 
{if in_array($Author[Author].Id, $ListAths.AuthorId)}
selected
{/if}
>
{$Author[Author].AuthorName}</option>
{/section}