Php

How To Add Elements to an Array in PHP?

This PHP tutorial help to add an element or array of elements into an array. PHP has a number of built-in functions to add elements into an array. We can combine two or more arrays to create a new (all-combination) array.

Pushing a new element into an array, adding one array to another array, merging two or more arrays together, and so on are all examples of array append.

In a PHP application, there are the following real-time conditions in which you must add elements to an array –

  • Add elements into the empty array.
  • Add elements into the existing array.
  • Add elements to the End of an Array in PHP.
  • Add elements at the beginning of an array in PHP.

Also checkout other related tutorials,

Let’s discuss one by one scenario to add items into an array –

Add Elements into the Empty array in PHP

The following methods can be used to add single or multiple elements to an empty array:

$arr = array(1);
//multiple elements
$arr = array(1, 2, 3);
//OR
$arr[] = 1;
$arr[] = 2;
echo "<pre>";print_r($arr);

Output :

Array
(
    [0] => 1
    [1] => 2
    [2] => 3
    [3] => 1
    [4] => 2
)

Add elements into the existing array in PHP

You can add items to the existing array using the following ways into PHP –

Related Post
//Numeric key
$arr[3] = 4;
echo "
<pre>";print_r($arr);

//Text key (assoc)
$arr['key'] = 'phpflow';
print_r($arr);

Output :

Array
(
    [3] => 4
)
Array
(
    [3] => 4
    [key] => phpflow
)

Add Elements Using array_unshift()

The PHP’s array_unshift() function is used to add elements to the beginning or starting of an array. It takes two arguments one is the array and the other is any number of elements you would like to add to the array.

$arr = array();
array_unshift($arr, 1);
array_unshift($arr, 2);
echo "
<pre>";print_r($arr);
// Or 

array_unshift($arr, 3, 4);
print_r($arr);

Output:

Array
(
    [0] => 2
    [1] => 1
)
Array
(
    [0] => 3
    [1] => 4
    [2] => 2
    [3] => 1
)

How to add elements to the end of an array in PHP

The array_push() function to insert one or more elements or values at the end of an array. You can add items at the end of an array in php using array_push method :

$arr = array();
array_push($arr, 1);
array_push($arr, 2);
echo "
<pre>";print_r($arr);

// Or
array_push($arr, 3, 4);
print_r($arr);

Output:

Array
(
    [0] => 1
    [1] => 2
)
Array
(
    [0] => 1
    [1] => 2
    [2] => 3
    [3] => 4
)

Append Elements to Array Using array_splice

The array_splice function can be used to add elements, remove elements, and/or replace elements in an array.

Syntax
array_splice(array, start, length, array)

  • array: Required! Specifies an array
  • start: Required! Where the function will start to insert or remove elements.
  • length: Required! How many elements will be inserted/removed.
  • array: Optional! inserted to the original array.
$my_arr = array(1, 4, 6, 7);
array_splice($my_arr, 2, 0, [11, 13] );
print_r($my_arr);

Output:

Array
(
    [0] => 1
    [1] => 4
    [2] => 11
    [3] => 13
    [4] => 6
    [5] => 7
)
Tags: php array

Recent Posts

Configure and Retrieve S3 Information Using Laravel PHP-AWS-SDK

This tutorial helps integrate a PHP SDK with Laravel. We'll install aws-php-sdk into laravel application and access all aws services… Read More

3 months ago

What is the Purpose of php_eol in PHP?

in this quick PHP tutorial, We'll discuss php_eol with examples. PHP_EOL is a predefined constant in PHP and represents an… Read More

9 months ago

Laravel Table Relationship Methods With Example

This Laravel tutorial helps to understand table Relationships using Elequonte ORM. We'll explore laravel table Relationships usage and best practices… Read More

9 months ago

Exploring the Power of Laravel Eloquent Join?

We'll explore different join methods of Laravel eloquent with examples. The join helps to fetch the data from multiple database… Read More

9 months ago

Quick and Easy Installation of Laravel Valet

in this Laravel tutorial, We'll explore valet, which is a development environment for macOS minimalists. It's a lightweight Laravel development… Read More

10 months ago

What is Laravel Soft Delete and How Does it Work?

I'll go through how to use soft delete in Laravel 10 in this post. The soft deletes are a method… Read More

10 months ago

Categories