• Skip to primary navigation
  • Skip to main content
  • Skip to primary sidebar

The Programming Expert

Solving All of Your Programming Headaches

  • Home
  • Learn to Code
    • Python
    • JavaScript
  • Code Snippets
    • HTML
    • JavaScript
    • jQuery
    • PHP
    • Python
    • SAS
    • Ruby
  • About
You are here: Home / PHP / How to Add Items to Array in php

How to Add Items to Array in php

March 23, 2022 Leave a Comment

In php, we can add items to arrays easily. If you want to add items to the end of an array, you can use the php array_push() function.

$example = array("lion");

array_push($example, "bear", "snake", "horse");

print_r($example);

//Output:
Array
(
    [0] => lion
    [1] => bear
    [2] => snake
    [3] => horse
)

If you want to add items at the beginning of an array, you can use the php array_unshift() function.

$example = array("lion");

array_unshift($example, "bear", "snake", "horse");

print_r($example);

//Output:
Array
(
    [0] => bear
    [1] => snake
    [2] => horse
    [3] => lion
)

When working with arrays and collections of data in php, it is useful to be able to add or remove items from our data structures easily.

We can easily add to arrays in php.

If you want to add items to the end of an array, we can use the php array_push() function.

array_push() takes an array and elements you want to add to the end of the array, and appends them to the array.

Below is an example of how to use array_push() to add to an array at the end of the array in php.

$example = array("lion");

array_push($example, "bear", "snake", "horse");

print_r($example);

//Output:
Array
(
    [0] => lion
    [1] => bear
    [2] => snake
    [3] => horse
)

How to Add Items at Beginning of Array with array_unshift() in php

If you want to prepend items at the beginning of an array, you can use the php array_unshift() function.

array_unshift() takes an array and elements you want to add to the beginning of the array, and prepends them to the array.

Below is an example of how to use array_unshift() to add to an array at the beginning of the array in php.

$example = array("lion");

array_unshift($example, "bear", "snake", "horse");

print_r($example);

//Output:
Array
(
    [0] => bear
    [1] => snake
    [2] => horse
    [3] => lion
)

Adding to an Empty Array in php

One example of using the array_push() function is to add to an empty array. As we know, we can add elements to an array with php array_push() function.

Therefore, after initializing an empty array, adding elements is easy.

Below is a simple example in php of creating an empty array and adding three elements to the array.

$emptyArray = [];

print_r($emptyArray);

array_push($emptyArray,"bear","snake","horse");

print_r($emptyArray);

//Output:
Array
(
)
Array
(
    [0] => bear
    [1] => snake
    [2] => horse
)

Hopefully this article has been useful for you to learn how to use php to add to arrays.

Other Articles You'll Also Like:

  • 1.  Add Months to Date in php
  • 2.  Using isset() Function in php to Check if Variable is Defined
  • 3.  php Absolute Value with abs Function
  • 4.  php atanh – Find Hyperbolic Arctangent of Number Using atanh() Function
  • 5.  php sleep() Function – Pause Execution of Code in php
  • 6.  php Rename File – Renaming Files and Directories Easily
  • 7.  Using php to Copy Files Easily from One Directory to Another
  • 8.  php Move File from One Directory to Another Folder
  • 9.  php switch case – How to Use Switch…Case Statements in php
  • 10.  php var_dump() Function – Print Information about Variable Type and Value

About The Programming Expert

The Programming Expert is a compilation of a programmer’s findings in the world of software development, website creation, and automation of processes.

Programming allows us to create amazing applications which make our work more efficient, repeatable and accurate.

At the end of the day, we want to be able to just push a button and let the code do it’s magic.

You can read more about us on our about page.

Reader Interactions

Leave a Reply Cancel reply

Your email address will not be published. Required fields are marked *

Primary Sidebar

About The Programming Expert

the programming expert main image

Welcome to The Programming Expert. We are a group of US-based programming professionals who have helped companies build, maintain, and improve everything from simple websites to large-scale projects.

We built The Programming Expert to help you solve your programming problems with useful coding methods and functions in various programming languages.

Search

Learn Coding from Experts on Udemy

Looking to boost your skills and learn how to become a programming expert?

Check out the links below to view Udemy courses for learning to program in the following languages:

Copyright © 2023 · The Programming Expert · About · Privacy Policy