W3docs

PHP How to add to end of array?

In PHP, you can add an element to the end of an array using the array_push() function.

In PHP, you can add an element to the end of an array using the array_push() function. The function takes the array as its first argument and the element to add as its second argument. Here's an example:

Example of adding to end of an array with array_push() function in PHP

<?php

$fruits = ["apple", "banana", "cherry"];
array_push($fruits, "orange");

print_r($fruits);

<div class="alert alert-info flex not-prose"> Watch a course <span class="hidden md:block">Watch a video course </span> Learn object oriented PHP</div>

You can also use the short array syntax, and the append [] operator.

Example of adding to end of an array with short array syntax in PHP

<?php

$fruits = ["apple", "banana", "cherry"];
$fruits[] = "Orange";

print_r($fruits);

After these examples, the $fruits array will now contain the elements: "apple", "banana", "cherry", "orange".