PHP Add Array Items

0
7K

Add Array Item

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

ExampleGet your own PHP Server

Add one more item to the fruits array:

$fruits = array("Apple", "Banana", "Cherry");
$fruits[] = "Orange";
Try it Yourself »

Associative Arrays

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

Example

Add one item to the car array:

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

Add Multiple Array Items

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

Example

Add three item to the fruits array:

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

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];
Try it Yourself »
Buscar
Categorías
Read More
Other
PHP Magic Constants
PHP Predefined Constants PHP has nine predefined constants that change value depending on where...
By PHP Tutorial 2024-05-17 07:40:16 0 5K
Other
PHP Sorting Arrays
The elements in an array can be sorted in alphabetical or numerical order, descending or...
By PHP Tutorial 2024-05-17 08:06:46 0 8K
Other
PHP - Escape Characters
Escape Character To insert characters that are illegal in a string, use an escape character. An...
By PHP Tutorial 2024-05-17 07:32:36 0 11K
Other
PHP Constants
Constants are like variables, except that once they are defined they cannot be changed or...
By PHP Tutorial 2024-05-17 07:39:53 0 8K
Other
PHP Casting
Sometimes you need to change a variable from one data type into another, and sometimes you want a...
By PHP Tutorial 2024-05-17 07:38:02 0 4K
Sociallez https://sociallez.com