How to push item to first index of array PHP

Summary:

If you are wanting to push a new item to the first element of an array you can do so by using PHP's function array_unshift(). This requires an existing array $arr for example and the item you want to put add to the first element $item for example, and achieved by using array_unshift($arr, $item).

Push item to first index of array PHP

PHP arrays are dynamic, meaning that you can push items to an array dynamically in runtime. The size of a dynamic array can expand over time. Arrays are thus super helpful for keeping multiple items.

add element to an array

PHP array_push function pushes one or more items to an array. Similarly, PHP array_unshift prepends one or more items to an array. This function is super helpful in the implementation of a queue using arrays. 

This article mainly explains how to push item to first index of array PHP. However, the coming section is a quick data structure detour. You can skip that section and jump straight to the main topic. However, we recommend going through the whole article to get the most out of it.

Queue Data structure – A detour

Have you seen queues in supermarkets – right? This data structure is built around the same concept. A queue is a ‘First In First Out’ or FIFO data structure. Thus, come into the counter first and get checked out first. So, a queue has a front and a rear.
The items are added to the rear and pushed out from the front. In queue terminologies, the add operation is ‘Enqueue’, and the remove operation is ‘Dequeue’.

Push item to first index of array PHP

So, the array is ideal for implementing a queue data structure. PHP array_unshift does ‘Enqueue’, and PHP array_pop does ‘Dequeue’.

A queue data structure plays a crucial role in building event-driven applications. Events are enqueued and dequeued as per the algorithm working behind the scenes. Can you think of a scenario where the queue data structure fits perfectly?

How to push item to first index of array PHP

PHP array_unshift function prepends one or more elements to an array. Following is the function signature.

array_unshift(array &$array, mixed ...$values): int

Arguments

  • $array – The input array
  • $values – The value to prepend

Example

Following is an example of using array_unshift in PHP.

<?php
$programming_languages = ['Java', 'Javascript', 'Go', 'Python', 'C#'];
 
array_unshift($programming_languages, 'PHP');
 
/*
OUTPUT
['PHP', 'Java', 'Javascript', 'Go', 'Python', 'C#']
*/
?>

Voila! Using the array_unshift function we add ‘PHP’ and ‘C++’ to the array of programming languages.

You can also add more than one element with array_unshift.

array_unshift($programming_languages, 'PHP', ‘C++’, ‘C’);

Implement a queue with array

Now as we have seen array_unshift, let’s see how to implement a queue.

<?php
class Queue {
    protected $arr = [];
 
   public function __construct($arr) {
        $this->arr = $arr;
   }
 
   //Returns the queue
   function getQueue() {
       return $this->arr;
   }
 
   function enqueue($item)
   {
       array_unshift($this->arr, $item);
   }
 
   function dequeue($dequeue)
   {
       array_pop($this->arr);
   }
}
?>

This example provides a basic Queue class. This Queue class has a constructor and several methods. Classes, constructors and methods are OOP concepts and out of scope for this article.

Here let’s initialize the Queue and do some operations on it.

$queue = new Queue([5,6,7]); //['5', '6', '7']
 
$queue->enqueue(4); // ['4', '5', '6', '7']
 
$queue->dequeue(); // ['4', '5', '6']
 
$queue->enqueue(3); // ['3', '4', '5', '6']
 
$queue->dequeue(); // ['3', '4', '5']

Super! You can try this yourself and add more to the Queue class. 

Conclusion

This article explains how to push item to first index of array PHP. It also includes a section about queue – a data structure commonly used in real-world applications. Finally, the article includes a section where we implement a queue data structure using a PHP array.

That’s all for this article, hope you’ve liked it. To see more articles like that stay tuned at FuelingPHP.

Want to learn more about PHP?

We have many fun articles related to PHP. You can explore these to learn more about PHP.


Article Categories

Article Tags

© 2022 Confident.Systems