Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    text
    copied!<p>I think you're looking for <a href="http://php.net/manual/en/function.array-fill.php" rel="noreferrer"><code>array_fill</code></a>:</p> <blockquote> <pre><code>array array_fill ( int $start_index , int $num , mixed $value ) </code></pre> <p>Fills an array with <code>num</code> entries of the value of the <code>value</code> parameter, keys starting at the <code>start_index</code> parameter.</p> </blockquote> <p>So:</p> <pre><code>$newElements = array_fill(0, $n, Array('val' =&gt; 0)); </code></pre> <p>You do still have to handle the appending of <code>$newElements</code> to <code>$existingArray</code> yourself, probably with <a href="http://www.php.net/manual/en/function.array-merge.php" rel="noreferrer"><code>array_merge</code></a>:</p> <blockquote> <pre><code>array array_merge ( array $array1 [, array $... ] ) </code></pre> <p>Merges the elements of one or more arrays together so that the values of one are appended to the end of the previous one. It returns the resulting array.</p> <p>If the input arrays have the same string keys, then the later value for that key will overwrite the previous one. If, however, the arrays contain numeric keys, the later value will <strong><em>not</em></strong> overwrite the original value, but will be appended.</p> <p>Values in the input array with numeric keys will be renumbered with incrementing keys starting from zero in the result array. </p> </blockquote> <p>So:</p> <pre><code>$existingArray = array_merge($existingArray, $newElements); </code></pre> <p>This all works because your top-level arrays are numerically-indexed.</p>
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload