While working on a database using PHP and MySQL, you, most of the time, have to collect new information about the user behavior and therefore need to insert the new data to a pre-existing array holding the data of the user. In that regard, there is a specific function in PHP to overcome the hardship of inserting new elements to the end of an array, known as the array_push function. In this tutorial, I will show you how to add new elements to the end of an array using the array_push function without any effort.
array_push() treats array as a stack, and pushes the passed variables onto the end of array. The length of array increases by the number of variables pushed.(1)
Define an array named data.
Add one element or more to the end of the data using the array_push function as explained below.
$data = array("orange", "banana");
array_push($data, "apple", "raspberry", "watermelon", "cherry", "strawberry");
Array (  => orange  => banana  => apple  => raspberry  => watermelon  => cherry  => strawberry )