Appending to an Array in PHP


Add an element to the end of an array with array_push() or simply using [].

Source Code

$fruits = ['apple', 'banana'];
$fruits[] = 'cherry'; // Simpler and preferred way
array_push($fruits, 'date'); // Alternative way
print_r($fruits);
Subscribe
Notify of
guest
0 Comments
Inline Feedbacks
View all comments