00 Votes

PHP: Insert new value at beginning of array

Question by Guest | 2014-06-12 at 19:53

I have a PHP script in which I am working with some array operations. I am used to work with so-called dynamic arrays from other programming languages, in which it is possible to add elements to an array even after you have created it for the first time.

It seems to me, that this is not so easy in PHP, because here you can only use something like array(1, 2, 3) to be able to create a new array. After that the number of array elements seems to be untouchable.

So, my question is, whether there is any possibility to expand an array created in this way afterwards? More concretely, I would like to insert a new value respectively element directly at the beginning of my array.

0Best Answer0 Votes

I am always using the function array_merge to be able to add new elements to an array. With array_merge, you can join together several arrays.

You can use it, for example, like that:

// Example 1
$arr = array(2, 3, 4, 5);
$arr = array_merge(array(1), $arr);

// Example 2
$arr = array(3, 4, 5);
$arr = array_merge(array(1, 2), $arr);

// Example 3
$arr = array(1, 2, 3, 4);
$arr = array_merge($arr, array(5));

After all, in each of my 3 examples, the result is an array containing the elements 1, 2, 3, 4 and 5. In the first two examples, we are inserting elements at the beginning, the last example shows, that you can also use this method to append elements at the end of an array.

The principle is always the same: we are just combining the old array with a new one that is containing the elements, we would like to add.
2014-06-12 at 20:34

ReplyPositive Negative
00 Votes

PHP is providing the function array_unshift, with which you can insert one or multiple new elements at the beginning of an array.

// insert one element at the beginning
$arr = array(2, 3, 4, 5);
$arr = array_unshift($arr, 1);     // 1, 2, 3, 4, 5

// insert two elements at the beginning
$arr = array(3, 4, 5);
$arr = array_unshift($arr, 1, 2);  // 1, 2, 3, 4, 5

So, the function expects at least two parameters: The array, to which the elements should be added as a first parameter and an arbitrary number of elements that should be inserted after that.
2014-06-13 at 23:08

ReplyPositive Negative
00 Votes

As another solution, I would like to introduce a somewhat unusual method. In some situations, even the function array_pad can be useful - depending on situation and purpose. This function is working similarly to array_fill and can be used to fill a passed array with the same value up to a given length.

$arr = array(1, 2, 3);
$arr = array_pad($arr, 6, 0);  // 1, 2, 3, 0, 0, 0

As first parameter, we are passing the original array. The second parameter is our desired length and the third parameter is the element or value that should be used for filling. In the example above, we would like to produce an array with 6 elements and the value 0 should be used for this.

With using a positive second parameter, it is filled at the back. With specifying a negative second parameter, it is filled at the front: 

$arr = array(1, 2, 3);
$arr = array_pad($arr, -6, 0);  // 0, 0, 0, 1, 2, 3

If we only want to insert one element at the beginning, a dynamic solution could look something like that:

$arr = array(1, 2, 3);
$arr = array_pad($arr, (count($arr)+1)*(-1), 0);  
// 0, 1, 2, 3

So, we only need to the negative length of the current array + 1, to insert exactly one element at the beginning. The expression (count($arr)+1)*(-1) dynamically cares about that.
2014-06-14 at 18:53

ReplyPositive Negative

Related Topics

Important Note

Please note: The contributions published on askingbox.com are contributions of users and should not substitute professional advice. They are not verified by independents and do not necessarily reflect the opinion of askingbox.com. Learn more.


Ask your own question or write your own article on askingbox.com. That’s how it’s done.