Add Array Items in PHP

You can add items to an array using the array's index or key. For indexed arrays, you can add items to the end using the array_push() function or by assigning a value to the next available index.

Example of Adding Array Items

<?php
$fruits = array("apple", "banana");
array_push($fruits, "orange");
$fruits[] = "grape";
print_r($fruits);

$person = array("name" => "John");
$person['age'] = 30;
print_r($person);
?>

Output:

Array
(
    [0] => apple
    [1] => banana
    [2] => orange
    [3] => grape
)
Array
(
    [name] => John
    [age] => 30
)

Explanation: This example demonstrates how to add items to both indexed and associative arrays using the array_push() function and by assigning values to keys.