PHP Add Array Items

0
7χλμ.

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 »
Αναζήτηση
Κατηγορίες
Διαβάζω περισσότερα
άλλο
PHP if...else Statements
PHP - The if...else Statement The if...else statement executes some code if a...
από PHP Tutorial 2024-05-17 07:46:12 0 4χλμ.
άλλο
PHP Continue
The continue statement can be used to jump out of the current iteration of a loop, and...
από PHP Tutorial 2024-05-17 07:52:32 0 4χλμ.
άλλο
PHP Variables Scope
PHP Variables Scope In PHP, variables can be declared anywhere in the script. The scope of a...
από PHP Tutorial 2024-05-17 07:18:52 0 10χλμ.
άλλο
PHP Access Arrays
Access Array Item To access an array item, you can refer to the index number for indexed arrays,...
από PHP Tutorial 2024-05-17 08:02:32 0 8χλμ.
άλλο
PHP Associative Arrays
PHP Associative Arrays Associative arrays are arrays that use named keys that you assign to...
από PHP Tutorial 2024-05-17 08:01:14 0 8χλμ.
Sociallez https://sociallez.com