PHP Add Array Items

0
7KB

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 while Loop
The while loop - Loops through a block of code as long as the specified condition is...
Por PHP Tutorial 2024-05-17 07:48:58 0 4KB
Outro
PHP Data Types
PHP Data Types Variables can store data of different types, and different data types can do...
Por PHP Tutorial 2024-05-17 07:27:24 0 5KB
Outro
PHP if Statements
Conditional statements are used to perform different actions based on different conditions....
Por PHP Tutorial 2024-05-17 07:45:23 0 4KB
Outro
PHP Associative Arrays
PHP Associative Arrays Associative arrays are arrays that use named keys that you assign to...
Por PHP Tutorial 2024-05-17 08:01:14 0 8KB
Outro
PHP $GLOBALS
$GLOBALS is an array that contains all global variables. Global Variables Global...
Por PHP Tutorial 2024-05-17 08:08:27 0 7KB
Sociallez https://sociallez.com