Curriculum
Course: PHP Basic
Login

Curriculum

PHP Basic

PHP Install

0/1

PHP Casting

0/1

PHP Constants

0/1

PHP Magic Constants

0/1

PHP Operators

0/1

PHP Reference

0/276
Text lesson

Add Array Items

Add Array Item

To add items to an existing array, you can use the bracket [] syntax.

Example

Add an additional item to the fruits array.

$fruits = array("Apple", "Banana", "Cherry");
$fruits[] = "Orange";

Associative Arrays

To add items to an associative array (or key/value array), use brackets [] for the key and assign the value using the = operator.

Example

Add a single item to the car array.

$cars = array("brand" => "Ford", "model" => "Mustang");
$cars["color"] = "Red";

 

Add Multiple Array Items

To add multiple items to an existing array, use the array_push() function.

Example

Add three items to the fruits array.

$fruits = array("Apple", "Banana", "Cherry");
array_push($fruits, "Orange", "Kiwi", "Lemon");

Add Multiple Items to Associative Arrays

To add multiple items to an existing array, you can use the += operator.

Example

Add two items to the cars array.

$cars = array("brand" => "Ford", "model" => "Mustang");
$cars += ["color" => "red", "year" => 1964];