企业🤖AI智能体构建引擎,智能编排和调试,一键部署,支持私有化部署方案 广告
<table summary="Header navigation table" width="100%" border="0" cellpadding="0" cellspacing="0"><tbody><tr><th colspan="3" align="center">Smarty - the compiling PHPtemplate engine</th> </tr><tr><td width="25%" align="left" valign="bottom"><a href="api.functions.html" accesskey="P">Prev</a></td> <td width="50%" align="center" valign="bottom">Chapter 13. Smarty Class Methods()</td> <td width="25%" align="right" valign="bottom"><a href="api.append.by.ref.html" accesskey="N">Next</a></td> </tr></tbody></table> # append()[添加] Name 函数名称 append() ― append an element to an assigned array 在指定数组中添加一个元素。 Description 描述 void append(mixed var); void append(string varname, mixed var, bool merge); If you append to a string value, it is converted to an array value and then appended to. You can explicitly pass name/value pairs, or associative arrays containing the name/value pairs. If you pass the optional third parameter of TRUE, the value will be merged with the current array instead of appended. 如果添加的是一个字符串,该字符串会被转换为数组格式后再进行添加。所添加的数据可以采用名/值对的格式,或者采用包含名/值对的关联数组格式。如果第三个可选参数被指定为TRUE,所添加的值会和数组中现有元素进行合并,而不是直接添加。 <table width="80%" border="0" cellpadding="2" cellspacing="2" class="note"><caption> 技术提示 </caption> <tr><td><p><b>Technical Note: </b> The merge parameter respects array keys, so if you merge two numerically indexed arrays,they may overwrite each other or result in non-sequential keys. This is unlike the PHP array_merge() [http://php.net/array_merge] function which wipes out numerical keys and renumbers them.</p> <p>使用第三个参数“merge”涉及到数组键,所以,如果添加和被添加的数组都是以数字为索引,他们会互相覆盖,或者产生不连续的索引。这并不象PHP中的<a href="http://php.net/array_merge">array_merge()</a>函数,后者会删除原有的数字索引,重新对索引进行编号。</p></td> </tr></table> <table class="EXAMPLE" cellspacing="0" cellpadding="0" border="0" width="100%"><tbody><tr><td><div class="EXAMPLE"> <a name="AEN3131" id="AEN3131"> </a> <b>Example 13-1. append()<br/></b> <table width="100%" bgcolor="#e0e0e0" border="0"><tbody><tr><td><p>&lt;?php<br/> // This is effectively the same as assign()<br/> $smarty-&gt;append('foo', 'Fred');<br/> // After this line, foo will now be seen as an array in the template<br/> $smarty-&gt;append('foo', 'Albert');</p> <p>$array = array(1 =&gt; 'one', 2 =&gt; 'two');<br/> $smarty-&gt;append('X', $array);<br/> $array2 = array(3 =&gt; 'three', 4 =&gt; 'four');<br/> // The following line will add a second element to the X array<br/> $smarty-&gt;append('X', $array2);</p> <p>// passing an associative array<br/> $smarty-&gt;append(array('city' =&gt; 'Lincoln', 'state' =&gt; 'Nebraska'));<br/> ?&gt;</p> </td> </tr></tbody></table><p><strong>译者例:关于第三个参数'merge'</strong></p> <table width="100%" bgcolor="#e0e0e0" border="0"><tbody><tr><td><p>上面提到'merge'涉及到数值合并的问题,这主要是针对添加的同是数组的情况:<br/> test.php:<br/> include_once('../libs/Smarty.class.php');<br/> $smarty = new Smarty;<br/> $smarty-&gt;assign('arr',array(1,2,3));<br/> $smarty-&gt;append('arr',array(4),true);<br/> $smarty-&gt;display('test.html');<br/><br/> test.html:<br/> {foreach $arr as $i}<br/>{$i}、<br/>{/foreach}<br/><br/><br/> 输出:4、2、3、</p> <p>将$smarty-&gt;append('arr',array(4),true);中的;;‘merge’的true参数改false则会完成添加:<br/> 1、2、3、Array、</p> </td> </tr></tbody></table></div></td> </tr></tbody></table> <table summary="Footer navigation table" width="100%" border="0" cellpadding="0" cellspacing="0"><tbody><tr><td width="33%" align="left" valign="top"><a href="api.functions.html" accesskey="P">Prev</a></td> <td width="34%" align="center" valign="top"><a href="index.html" accesskey="H">Home</a></td> <td width="33%" align="right" valign="top"><a href="api.append.by.ref.html" accesskey="N">Next</a></td> </tr><tr><td width="33%" align="left" valign="top">Smarty Class Methods()<br/> Smarty类方法 </td> <td width="34%" align="center" valign="top"><a href="api.functions.html" accesskey="U">Up</a></td> <td width="33%" align="right" valign="top">appendByRef()<br/> 引用添加 </td> </tr></tbody></table>