<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd"> <html> <head> <meta http-equiv="content-type" content="text/html; charset=UTF-8"> <title>Fill an array with values</title> </head> <body><div class="manualnavbar" style="text-align: center;"> <div class="prev" style="text-align: left; float: left;"><a href="function.array-fill-keys.html">array_fill_keys</a></div> <div class="next" style="text-align: right; float: right;"><a href="function.array-filter.html">array_filter</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-fill" class="refentry"> <div class="refnamediv"> <h1 class="refname">array_fill</h1> <p class="verinfo">(PHP 4 >= 4.2.0, PHP 5)</p><p class="refpurpose"><span class="refname">array_fill</span> — <span class="dc-title">Fill an array with values</span></p> </div> <div class="refsect1 description" id="refsect1-function.array-fill-description"> <h3 class="title">Description</h3> <div class="methodsynopsis dc-description"> <span class="type">array</span> <span class="methodname"><strong>array_fill</strong></span> ( <span class="methodparam"><span class="type">int</span> <code class="parameter">$start_index</code></span> , <span class="methodparam"><span class="type">int</span> <code class="parameter">$num</code></span> , <span class="methodparam"><span class="type"><a href="language.pseudo-types.html#language.types.mixed" class="type mixed">mixed</a></span> <code class="parameter">$value</code></span> )</div> <p class="para rdfs-comment"> Fills an array with <em><code class="parameter">num</code></em> entries of the value of the <em><code class="parameter">value</code></em> parameter, keys starting at the <em><code class="parameter">start_index</code></em> parameter. </p> </div> <div class="refsect1 parameters" id="refsect1-function.array-fill-parameters"> <h3 class="title">Parameters</h3> <p class="para"> <dl> <dt> <span class="term"><em><code class="parameter">start_index</code></em></span> <dd> <p class="para"> The first index of the returned array. </p> <p class="para"> If <em><code class="parameter">start_index</code></em> is negative, the first index of the returned array will be <em><code class="parameter">start_index</code></em> and the following indices will start from zero (see <a href="function.array-fill.html#function.array-fill.example.basic" class="link">example</a>). </p> </dd> </dt> <dt> <span class="term"><em><code class="parameter">num</code></em></span> <dd> <p class="para"> Number of elements to insert. Must be greater than zero. </p> </dd> </dt> <dt> <span class="term"><em><code class="parameter">value</code></em></span> <dd> <p class="para"> Value to use for filling </p> </dd> </dt> </dl> </p> </div> <div class="refsect1 returnvalues" id="refsect1-function.array-fill-returnvalues"> <h3 class="title">Return Values</h3> <p class="para"> Returns the filled array </p> </div> <div class="refsect1 errors" id="refsect1-function.array-fill-errors"> <h3 class="title">Errors/Exceptions</h3> <p class="para"> Throws a <strong><code>E_WARNING</code></strong> if <em><code class="parameter">num</code></em> is less than one. </p> </div> <div class="refsect1 examples" id="refsect1-function.array-fill-examples"> <h3 class="title">Examples</h3> <p class="para"> <div class="example" id="function.array-fill.example.basic"> <p><strong>Example #1 <span class="function"><strong>array_fill()</strong></span> example</strong></p> <div class="example-contents"> <div class="phpcode"><code><span style="color: #000000"> <span style="color: #0000BB"><?php<br />$a </span><span style="color: #007700">= </span><span style="color: #0000BB">array_fill</span><span style="color: #007700">(</span><span style="color: #0000BB">5</span><span style="color: #007700">, </span><span style="color: #0000BB">6</span><span style="color: #007700">, </span><span style="color: #DD0000">'banana'</span><span style="color: #007700">);<br /></span><span style="color: #0000BB">$b </span><span style="color: #007700">= </span><span style="color: #0000BB">array_fill</span><span style="color: #007700">(-</span><span style="color: #0000BB">2</span><span style="color: #007700">, </span><span style="color: #0000BB">4</span><span style="color: #007700">, </span><span style="color: #DD0000">'pear'</span><span style="color: #007700">);<br /></span><span style="color: #0000BB">print_r</span><span style="color: #007700">(</span><span style="color: #0000BB">$a</span><span style="color: #007700">);<br /></span><span style="color: #0000BB">print_r</span><span style="color: #007700">(</span><span style="color: #0000BB">$b</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 screen"> <div class="cdata"><pre> Array ( [5] => banana [6] => banana [7] => banana [8] => banana [9] => banana [10] => banana ) Array ( [-2] => pear [0] => pear [1] => pear [2] => pear ) </pre></div> </div> </div> </p> </div> <div class="refsect1 notes" id="refsect1-function.array-fill-notes"> <h3 class="title">Notes</h3> <p class="para"> See also the <a href="language.types.array.html" class="link">Arrays</a> section of manual for a detailed explanation of negative keys. </p> </div> <div class="refsect1 seealso" id="refsect1-function.array-fill-seealso"> <h3 class="title">See Also</h3> <p class="para"> <ul class="simplelist"> <li class="member"> <span class="function"><a href="function.array-fill-keys.html" class="function" rel="rdfs-seeAlso">array_fill_keys()</a> - Fill an array with values, specifying keys</span></li> <li class="member"> <span class="function"><a href="function.str-repeat.html" class="function" rel="rdfs-seeAlso">str_repeat()</a> - Repeat a string</span></li> <li class="member"> <span class="function"><a href="function.range.html" class="function" rel="rdfs-seeAlso">range()</a> - Create an array containing a range of elements</span></li> </ul> </p> </div> </div><hr /><div class="manualnavbar" style="text-align: center;"> <div class="prev" style="text-align: left; float: left;"><a href="function.array-fill-keys.html">array_fill_keys</a></div> <div class="next" style="text-align: right; float: right;"><a href="function.array-filter.html">array_filter</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>