How to add element to php array

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'];

or

$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

$element='d';

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 )




Leave a reply

Latest Posts

10 Best Malayalam Movies You Must Watch in 2024

10 Best Malayalam Movies You Must Watch in 2024

The Malayalam film industry is renowned for producing inventive and varied films, and 2024 is already looking to be no different. With so many different films slated for release—from high-profile blockbusters to indie productions—there's something for everyone to enjoy. Here we have listed the 10 best Malayalam movies that give you goosebumps and make you cry. Most of the Malayalam movies are written based on true stories and events.

Trend: Exevor launched new tool for domain availability check

Trend: Exevor launched new tool for domain availability check

We launched a tool for search domain name for check availability. This tool is little bit straight and quick.

Kiwifruit

Kiwifruit

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.