How to add element to php array

Webee Cafe
How to add element to php array

Adding an element to PHP array and removing an element from php array is very simple. PHP has in-built function to perform this. A function array_push() is used to add an element to PHP array

Creating an array:

$custom_array = ['a','b','c'];


$custom_array = array('a','b','c');

Add an element to an array:

In php array_push function used to add an element to array. Syntax of array_push is

array_push(array, var);

If you want to add 'd' to $custom_array


array_push($custom_array , $element);

Result is:

Array ( [0] => a [1] => b [2] => c [3] => d )

Remove an element from array:

If you want to remove 'b' from $custom_array use unset function:

unset($custom_array [1]);

Result is 

Array ( [0] => a [2] => c )

Latest Posts



Kiwifruit, also known simply as kiwi, is a small, fuzzy fruit with a brown, hairy skin and bright green flesh. It is known for its unique, sweet-tart flavor and vibrant green color. The scientific name for the most common variety is Actinidia deliciosa. Kiwifruit is native to China and was originally called Chinese gooseberry, but it was later renamed kiwifruit or kiwi in reference to the New Zealand national bird, the kiwi, which has a similar brown, fuzzy exterior.

Golden Temple Kushalnagar, Coorg's

Golden Temple Kushalnagar, Coorg's

The Golden Temple in Kushalnagar, Coorg, is officially known as the Namdroling Monastery. It is a significant Tibetan Buddhist monastery and is one of the largest teaching centers of the Nyingma lineage of Tibetan Buddhism outside Tibet.

Mysore Palace

Mysore Palace

The Mysore Palace, also known as the Amba Vilas Palace, is a historical palace located in the city of Mysore in the southern state of Karnataka, India. It is one of the most famous tourist attractions in the country and is renowned for its magnificent architecture and rich cultural heritage.

All about the King Cobra - 2 incidents, Power, Venom, Style, Speed

All about the King Cobra - 2 incidents, Power, Venom, Style, Speed

We all know that the king of the jungle is the lion. Because of its strength, majestic gait and roar that shakes the entire forest. Similarly, how much do you know about the snake king? Yes, its name itself says Raja Naga. That means King Cobra. Its scientific name is called ophiophagus hanna. It is the world's largest venomous snake. Its poison is so dangerous that once the poison from its mouth is distributed to 10 people, ten people will lose their lives.

Leave a reply

Exevor's Affordable Elegance: Top Picks for Budget-Savvy Shoppers

Discover unbeatable deals at Exevor Shop's featured product section! Explore a curated selection of budget-friendly yet high-quality items for the savvy shopper in you.