PHP Add Array Items

0
8KB

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 »
Rechercher
Catégories
Lire la suite
Autre
PHP Create Arrays
Create Array You can create arrays by using the array() function: ExampleGet your...
Par PHP Tutorial 2024-05-17 08:01:55 0 8KB
Autre
PHP Add Array Items
Add Array Item To add items to an existing array, you can use the bracket [] syntax....
Par PHP Tutorial 2024-05-17 08:06:05 0 8KB
Autre
PHP Indexed Arrays
PHP Indexed Arrays In indexed arrays each item has an index number. By default, the first item...
Par PHP Tutorial 2024-05-17 08:00:38 0 6KB
Autre
PHP Introduction
PHP code is executed on the server. What You Should Already Know Before you continue you...
Par PHP Tutorial 2024-05-17 07:08:40 0 5KB
Autre
PHP Functions
The real power of PHP comes from its functions. PHP has more than 1000 built-in functions, and...
Par PHP Tutorial 2024-05-17 07:53:06 0 5KB
Sociallez https://sociallez.com