Search code snippets, questions, articles...

Add item to array PHP

    $my_array = ["apple"];
    array_push($my_array, "orange");
Best JSON Validator, JSON Tree Viewer, JSON Beautifier at same place. Check how cool is the tool
["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

    $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?