PHP Add Array Items

0
5K

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 Syntax
A PHP script is executed on the server, and the plain HTML result is sent back to the browser....
Por PHP Tutorial 2024-05-17 07:10:53 0 5K
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 3K
Outro
PHP Numbers
In this chapter we will look in depth into Integers, Floats, and Number Strings. PHP Numbers...
Por PHP Tutorial 2024-05-17 07:37:05 0 4K
Outro
PHP - Escape Characters
Escape Character To insert characters that are illegal in a string, use an escape character. An...
Por PHP Tutorial 2024-05-17 07:32:36 0 7K
Outro
PHP Installation
What Do I Need? To start using PHP, you can: Find a web host with PHP and MySQL support...
Por PHP Tutorial 2024-05-17 07:09:40 0 3K