Chrome Extension

Add item to array PHP

<?php
    $my_array = ["apple"];
    array_push($my_array, "orange");
?>
We are a bunch of lazy programmers so we developed this chrome extension
search code from your favorite search engine
Install Now
Output
["apple", "orange"]

PHP inbuilt function array_push() can be used to add items to an array. It takes a minimum of two required parameters as $array_name which is the name of the array in which item to be added and $item which is to be added in the array.

You can pass more than one item to array_push to be added to the array like below

<?php
    $subject = ["Math", "Physics"];
    array_push($subject, "English", "Chemistry");
?>

Search Index Data (The code snippet can also be found with below search text)

array_push() function php
push item to array php
Was this helpful?
0 Comments
Open Code Manager We are hiring