Sick Gaming
[Tut] PHP array_push – Add Elements to an Array - Printable Version

+- Sick Gaming (https://www.sickgaming.net)
+-- Forum: Programming (https://www.sickgaming.net/forum-76.html)
+--- Forum: PHP Development (https://www.sickgaming.net/forum-82.html)
+--- Thread: [Tut] PHP array_push – Add Elements to an Array (/thread-100160.html)



[Tut] PHP array_push – Add Elements to an Array - xSicKxBot - 10-30-2022

PHP array_push – Add Elements to an Array

<div style="margin: 5px 5% 10px 5%;"><img src="https://www.sickgaming.net/blog/wp-content/uploads/2022/10/php-array_push-add-elements-to-an-array.jpg" width="550" height="413" title="" alt="" /></div><div><div class="modified-on" readability="7.1304347826087"> by <a href="https://phppot.com/about/">Vincy</a>. Last modified on October 30th, 2022.</div>
<p>Adding elements to an array in PHP is very easy with its native function <em>array_push()</em>.</p>
<p>This quick example shows the simplicity of this function to add more elements to the end of an array.</p>
<h2>Quick example</h2>
<pre class="prettyprint"><code class="language-php">&lt;?php
$animalsArray = array( "Lion", "Tiger"
);
array_push($animalsArray, "Elephant", "Horse");
print_r($animalsArray);
?&gt;
</code></pre>
<p><strong>Output:</strong></p>
<pre class="prettyprint"><code>Array ( [0] =&gt; Lion [1] =&gt; Tiger [2] =&gt; Elephant [3] =&gt; Horse )
</code></pre>
<h2>About PHP array_push()</h2>
<p>PHP array_push() function add elements to an array. It can add one or more trailing elements to an existing array.</p>
<p><strong>Syntax</strong></p>
<pre class="prettyprint"><code class="language-php">array_push(array &amp;$array, mixed ...$values): int
</code></pre>
<ul>
<li><em>$array</em> – The reference of a target array to push elements.</li>
<li><em>$values</em> – one or more elements to be pushed to the target array.</li>
</ul>
<p>When we see the <a href="https://phppot.com/php/power-of-php-arrays/">PHP array functions</a>, we have seen a short description of this function.</p>
<p><img loading="lazy" class="alignnone size-large wp-image-19952" src="https://phppot.com/wp-content/uploads/2022/10/php-array-push-550x413.jpg" alt="php array push" width="550" height="413" srcset="https://phppot.com/wp-content/uploads/2022/10/php-array-push-550x413.jpg 550w, https://phppot.com/wp-content/uploads/2022/10/php-array-push-300x225.jpg 300w, https://phppot.com/wp-content/uploads/2022/10/php-array-push-768x576.jpg 768w, https://phppot.com/wp-content/uploads/2022/10/php-array-push.jpg 960w" sizes="(max-width: 550px) 100vw, 550px"></p>
<h2>All possible ways of doing array push in PHP</h2>
<p>In this tutorial, we will see all the possibilities for adding elements to an array in PHP. Those are,</p>
<ul>
<li>Array push by assigning values to an array variable by key.</li>
<li>Pushing array elements in a loop.</li>
</ul>
<p>When seeing the examples, it will be very simple and may be too familiar also. But recollecting all the methods at one glance will help to rejuvenate the skillset on basics.</p>
<h2>How to add an array of elements to a target array using array_push()</h2>
<p>This example uses the PHP&nbsp;<em>array_push()</em> function to push an array of elements into a target array.</p>
<pre class="prettyprint"><code class="language-php">&lt;?php
$animalsArray = array( "Lion", "Tiger"
);
$anotherArray = array( "Elephant", "Crocodile"
); array_push($animalsArray, ...$anotherArray);
print_r($animalsArray);
// this method adds elements from two arrays sequentially into the target array
// this is similar to merge
?&gt;
</code></pre>
<p><strong>Output:</strong></p>
<pre class="prettyprint"><code>Array ( [0] =&gt; Lion [1] =&gt; Tiger [2] =&gt; Elephant [3] =&gt; Crocodile )
</code></pre>
<h2>The alternate method to array_push</h2>
<p>The array_push function is useful if there is a requirement to push elements later after the alignment.</p>
<p>If you want to push the elements at an assignment level the following code shows the way to do it.</p>
<p>If you want to merge <a href="https://phppot.com/php/php-json-array-merge/">JSON array or object using PHP</a> the linked article will be helpful.</p>
<pre class="prettyprint"><code class="language-php">&lt;?php
// alternate to use array_push when you have a key value
// add elements as key value via index
$animalsArray['a1'] = 'Lion';
$animalsArray['a2'] = 'Tiger';
$animalsArray['a3'] = 'Elephant';
$animalsArray['a4'] = 'Horse';
print_r($animalsArray);
?&gt;
</code></pre>
<p><strong>Output:</strong></p>
<pre class="prettyprint"><code>Array ( [a1] =&gt; Lion [a2] =&gt; Tiger [a3] =&gt; Elephant [a4] =&gt; Horse )
</code></pre>
<h2>Pushing elements into an array in a loop without using array_push</h2>
<p>This code is the same as above but with a <a href="https://phppot.com/php/loop-control-structure/">PHP for a loop</a>. It pushes only the value to the array variable with a square bracket.</p>
<p>The output will have the array with a numerical key.</p>
<pre class="prettyprint"><code class="language-php">&lt;?php
// another alternate to array_push
// add elements to an array with just []
$array = array();
for ($i = 1; $i &lt;= 10; $i ++) { $array[] = $i;
}
print_r($array);
?&gt;
</code></pre>
<p>If you want to push the key-value pair to form an associative array with a loop, the following code will be helpful.</p>
<p><strong>Output:</strong></p>
<pre class="prettyprint"><code>Array ( [0] =&gt; 1 [1] =&gt; 2 [2] =&gt; 3 [3] =&gt; 4 [4] =&gt; 5 [5] =&gt; 6 [6] =&gt; 7 [7] =&gt; 8 [8] =&gt; 9 [9] =&gt; 10 )
</code></pre>
<h2>Adding elements into an array using PHP array_merge()</h2>
<p>The&nbsp;<em>array_merge()</em> and the <em>array_push($array, …$array_sequence)</em> gives same output.</p>
<p>It merges two array variables and results in a consolidated element array. If you want to <a href="https://phppot.com/php/php-json-array-merge/">merge JSON array or object in PHP</a> the linked article has the code.</p>
<pre class="prettyprint"><code class="language-php">&lt;?php
$animalsArray = array( "Lion", "Tiger"
);
$moreAnimalsArray = array( "Elephant", "Horse"
);
// to add elements in an array from existing arrays
$array = array_merge($animalsArray, $moreAnimalsArray);
print_r($array);
?&gt;
</code></pre>
<p><strong>Output:</strong></p>
<pre class="prettyprint"><code>Array ( [0] =&gt; Lion [1] =&gt; Tiger [2] =&gt; Elephant [3] =&gt; Horse )
</code></pre>
<h2>PHP function to add elements to the beginning of an array</h2>
<p>PHP also contains functions to add elements to an array at the beginning of an array. The array_unshift() function is used for this. See the following code that adds elements to an array using array_shift().</p>
<pre class="prettyprint"><code class="language-php">&lt;?php
$animalsArray = array( "Lion", "Tiger"
);
array_unshift($animalsArray, "Elephant", "Horse");
print_r($animalsArray);
?&gt;
</code></pre>
<p><strong>Output:</strong></p>
<pre class="prettyprint"><code>Array ( [0] =&gt; Elephant [1] =&gt; Horse [2] =&gt; Lion [3] =&gt; Tiger )
</code></pre>
<p><a class="download" href="https://phppot.com/downloads/php/php-array-push.zip">Download</a></p>
<p> <!-- #comments --> </p>
<div class="related-articles">
<h2>Popular Articles</h2>
</p></div>
<p> <a href="https://phppot.com/php/php-array-push/#top" class="top">↑ Back to Top</a> </p>
</div>


https://www.sickgaming.net/blog/2022/10/29/php-array_push-add-elements-to-an-array/