PHP Add Array Items
Posted 2024-05-17 08:06:05
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 »Zoeken
Categorieën
- Art
- Causes
- Crafts
- Dance
- Drinks
- Film
- Fitness
- Food
- Spellen
- Gardening
- Health
- Home
- Literature
- Music
- Networking
- Other
- Party
- Religion
- Shopping
- Sports
- Theater
- Wellness
Read More
PHP Access Arrays
Access Array Item
To access an array item, you can refer to the index number for indexed arrays,...
PHP $GLOBALS
$GLOBALS is an array that contains all global variables.
Global Variables
Global...
PHP do while Loop
The do...while loop - Loops through a block of code once, and then repeats the loop as...
PHP Arrays
An array stores multiple values in one single variable:
ExampleGet your own PHP Server
$cars...
PHP if...else Statements
PHP - The if...else Statement
The if...else statement executes some code if a...