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 »
Search
Categories
Read More
Other
PHP Shorthand if Statements
Short Hand If To write shorter code, you can write if statements on one line....
By PHP Tutorial 2024-05-17 07:47:03 0 3K
Other
تطورات التعلم الآلي وتأثيرها على مستقبل العمل
مقدمة: في ظل التطورات السريعة للتكنولوجيا والابتكار، أصبح التعلم الآلي...
By MOHAMED ATTALLAH 2024-05-14 13:56:58 0 5K
Other
الثورة البلوكتشين وتحولاتها في الصناعات المختلفة
مقدمة: شهدت تقنية البلوكتشين ثورة هائلة في السنوات الأخيرة، حيث أصبحت لديها القدرة على تغيير...
By MOHAMED ATTALLAH 2024-05-14 14:03:03 0 5K
Other
PHP for Loop
The for loop - Loops through a block of code a specified number of times. The PHP...
By PHP Tutorial 2024-05-17 07:49:43 0 3K
Other
PHP Introduction
PHP code is executed on the server. What You Should Already Know Before you continue you...
By PHP Tutorial 2024-05-17 07:08:40 0 3K