function.array-push.html
来自「php的帮助文档,涉及到PHP的案例和基本语法,以及实际应用内容」· HTML 代码 · 共 151 行
HTML
151 行
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd"><html> <head> <title>Push one or more elements onto the end of array</title> <meta http-equiv="content-type" content="text/html; charset=UTF-8"> </head> <body><div style="text-align: center;"> <div class="prev" style="text-align: left; float: left;"><a href="function.array-product.html">array_product</a></div> <div class="next" style="text-align: right; float: right;"><a href="function.array-rand.html">array_rand</a></div> <div class="up"><a href="ref.array.html">Array Functions</a></div> <div class="home"><a href="index.html">PHP Manual</a></div></div><hr /><div id="function.array-push" class="refentry"> <div class="refnamediv"> <h1 class="refname">array_push</h1> <p class="verinfo">(PHP 4, PHP 5)</p><p class="refpurpose"><span class="refname">array_push</span> — <span class="dc-title">Push one or more elements onto the end of array</span></p> </div> <div class="refsect1 description"> <h3 class="title">Description</h3> <div class="methodsynopsis dc-description"> <span class="type">int</span> <span class="methodname"><b><b>array_push</b></b></span> ( <span class="methodparam"><span class="type">array</span> <tt class="parameter reference">&$array</tt></span> , <span class="methodparam"><span class="type"><a href="language.pseudo-types.html#language.types.mixed" class="type mixed">mixed</a></span> <tt class="parameter">$var</tt></span> [, <span class="methodparam"><span class="type"><a href="language.pseudo-types.html#language.types.mixed" class="type mixed">mixed</a></span> <tt class="parameter">$...</tt></span> ] )</div> <p class="para rdfs-comment"> <b>array_push()</b> treats <i><tt class="parameter">array</tt></i> as a stack, and pushes the passed variables onto the end of <i><tt class="parameter">array</tt></i>. The length of <i><tt class="parameter">array</tt></i> increases by the number of variables pushed. Has the same effect as: <div class="example-contents"><div class="phpcode"><code><span style="color: #000000"><span style="color: #0000BB"><?php<br />$array</span><span style="color: #007700">[] = </span><span style="color: #0000BB">$var</span><span style="color: #007700">;<br /></span><span style="color: #0000BB">?></span></span></code></div> </div> repeated for each <i><tt class="parameter">var</tt></i>. </p> <blockquote><p><b class="note">Note</b>: <span class="simpara"> If you use <b>array_push()</b> to add one element to the array it's better to use <i>$array[] = </i> because in that way there is no overhead of calling a function. </span> </p></blockquote> <blockquote><p><b class="note">Note</b>: <span class="simpara"> <b>array_push()</b> will raise a warning if the first argument is not an array. This differs from the <i>$var[]</i> behaviour where a new array is created. </span> </p></blockquote> </div> <div class="refsect1 parameters"> <h3 class="title">Parameters</h3> <p class="para"> <dl> <dt> <span class="term"><i><tt class="parameter">array</tt></i></span> <dd> <p class="para"> The input array. </p> </dd> </dt> <dt> <span class="term"><i><tt class="parameter">var</tt></i></span> <dd> <p class="para"> The pushed value. </p> </dd> </dt> </dl> </p> </div> <div class="refsect1 returnvalues"> <h3 class="title">Return Values</h3> <p class="para"> Returns the new number of elements in the array. </p> </div> <div class="refsect1 examples"> <h3 class="title">Examples</h3> <p class="para"> <div class="example"> <p><b>Example #1 <b>array_push()</b> example</b></p> <div class="example-contents"><div class="phpcode"><code><span style="color: #000000"><span style="color: #0000BB"><?php<br />$stack </span><span style="color: #007700">= array(</span><span style="color: #DD0000">"orange"</span><span style="color: #007700">, </span><span style="color: #DD0000">"banana"</span><span style="color: #007700">);<br /></span><span style="color: #0000BB">array_push</span><span style="color: #007700">(</span><span style="color: #0000BB">$stack</span><span style="color: #007700">, </span><span style="color: #DD0000">"apple"</span><span style="color: #007700">, </span><span style="color: #DD0000">"raspberry"</span><span style="color: #007700">);<br /></span><span style="color: #0000BB">print_r</span><span style="color: #007700">(</span><span style="color: #0000BB">$stack</span><span style="color: #007700">);<br /></span><span style="color: #0000BB">?></span></span></code></div> </div> <div class="example-contents"><p>The above example will output:</p></div> <div class="example-contents"><pre><div class="cdata"><pre>Array( [0] => orange [1] => banana [2] => apple [3] => raspberry)</pre></div> </pre></div> </div> </p> </div> <div class="refsect1 seealso"> <h3 class="title">See Also</h3> <p class="para"> <ul class="simplelist"> <li class="member"><a href="function.array-pop.html" class="function" rel="rdfs-seeAlso">array_pop()</a></li> <li class="member"><a href="function.array-shift.html" class="function" rel="rdfs-seeAlso">array_shift()</a></li> <li class="member"><a href="function.array-unshift.html" class="function" rel="rdfs-seeAlso">array_unshift()</a></li> </ul> </p> </div></div><hr /><div style="text-align: center;"> <div class="prev" style="text-align: left; float: left;"><a href="function.array-product.html">array_product</a></div> <div class="next" style="text-align: right; float: right;"><a href="function.array-rand.html">array_rand</a></div> <div class="up"><a href="ref.array.html">Array Functions</a></div> <div class="home"><a href="index.html">PHP Manual</a></div></div></body></html>
⌨️ 快捷键说明
复制代码Ctrl + C
搜索代码Ctrl + F
全屏模式F11
增大字号Ctrl + =
减小字号Ctrl + -
显示快捷键?