Skip to content

PHP Updating Indexed Arrays

Syntax

Elements in an indexed array in PHP can be updated by directly assigning a new value to the desired index.

$arrayName[index] = "newValue";

Example

// Declaring an indexed array
$fruits = ["Apple", "Banana", "Cherry"];
// Updating an element
$fruits[1] = "Blueberry";
// Adding a new element (at the next available index)
$fruits[] = "Dragonfruit";