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 »
Pesquisar
Categorias
Leia Mais
Outro
PHP Variables
Variables are "containers" for storing information. Creating (Declaring) PHP Variables In...
Por PHP Tutorial 2024-05-17 07:16:59 0 7K
Outro
PHP Array Functions
PHP Array Functions PHP has a set of built-in functions that you can use on arrays....
Por PHP Tutorial 2024-05-17 08:07:42 1 7K
Outro
PHP Break
The break statement can be used to jump out of different kind of loops. Break in For...
Por PHP Tutorial 2024-05-17 07:50:37 0 5K
Outro
PHP switch Statement
The switch statement is used to perform different actions based on different...
Por PHP Tutorial 2024-05-17 07:47:56 0 4K
Outro
PHP for Loop
The for loop - Loops through a block of code a specified number of times. The PHP...
Por PHP Tutorial 2024-05-17 07:49:43 0 4K
Sociallez https://sociallez.com