Create a PHP Array, Then Read and Update Its Items
Learn to create PHP arrays, read items by index or key, update a list item, and use foreach to print keys and values.
To store a list of items in PHP, create an indexed array and read an item with its numeric index, such as $items[0]. For a record with named fields, use an associative array and a string key, such as $record["name"]. The distinction matters when you update a value: changing index 0 in the list does not mean changing a field named name in the record.
Create a list and a record
Here is one example you can place in a PHP file. It uses square brackets for the list and array() for the record; both forms create arrays. The record’s => entries pair each field name with its value.
<?php
$items = ["notebook", "pen", "folder"];
$record = array("name" => "Maya", "item" => "notebook");
echo "First item: " . $items[0] . "\n";
echo "Record name: " . $record["name"] . "\n";
$items[0] = "planner";
echo "Updated first item: " . $items[0] . "\n";
foreach ($items as $index => $item) {
echo "Item " . $index . ": " . $item . "\n";
}
foreach ($record as $key => $value) {
echo "Field " . $key . ": " . $value . "\n";
}
$records = [
$record,
["name" => "Jules", "item" => "pen"],
];
echo "Second record name: " . $records[1]["name"] . "\n";
The leading <?php is included so the snippet stands as a PHP file rather than an isolated fragment. Nothing needs to be added to the example to define its arrays.
The first item in an indexed array has index 0, the second has index 1, and so on, as the W3Schools indexed-array guide describes. In this example, $items[0] initially retrieves notebook. $record["name"] instead uses the string key name and retrieves Maya. The two bracketed accesses look alike, but they point to different kinds of keys.
Read a field inside a nested array
The $records array is a list whose items are themselves records. In $records[1]["name"], read the brackets from left to right: [1] selects the second record, then ["name"] selects that record’s named field. The result is Jules, not Maya. A PHP array handbook example likewise reads named fields after selecting a record by numeric index.
This is a useful way to inspect a list of records without treating a field name as though it were a list position. $records[1] selects a whole record; the additional ["name"] narrows the access to one value in it.
Update the list, then visit its contents
The assignment $items[0] = "planner"; changes the list item at index 0. The later read of that same index therefore displays planner, while indexes 1 and 2 still identify pen and folder. The assignment does not change $record["item"]: that is a field in the separately defined record and remains notebook in this example.
Each foreach loop reads an array’s contents. In foreach ($items as $index => $item), $index identifies the visited numeric index and $item holds its value. The record loop uses the same pattern, but its $key values are field names. The handbook also demonstrates using foreach with => to retrieve a visited value alongside its key.
The example prints the following text, with each \n ending a line:
First item: notebook
Record name: Maya
Updated first item: planner
Item 0: planner
Item 1: pen
Item 2: folder
Field name: Maya
Field item: notebook
Second record name: Jules
Notice what the output lets you check: the direct read before the assignment says notebook, while both the direct read and the loop after it say planner for index 0. The record loop labels Maya with the key name and notebook with the key item. If an access retrieves the wrong value, identify its key first—numeric position for these lists, string field name for these records—then follow any additional brackets into the nested record.
