1 of 110 menu

Add to an Associative Array in PHP

Let's say we have the following associative array:

<?php $arr = ['a' => 1, 'b' => 2, 'c' => 3]; ?>

Let's add an element with the key 'd' and the value 4 to it:

<?php $arr = ['a' => 1, 'b' => 2, 'c' => 3]; $arr['d'] = 4; // add a new element to the array var_dump($arr); ?>

Code execution result:

['a' => 1, 'b' => 2, 'c' => 3, 'd' => 4]
English
БеларускаяEspañolРусский
We use cookies for website operation, analytics, and personalization. Data processing is carried out in accordance with the Privacy Policy.
accept all customize decline