PHP Access Array Items
In this page:
$array[0]; // indexed: first item
$array["key"]; // associative: by key
isset($array["key"]); // exists and not null
array_key_exists("key", $array); // exists
Accessing Indexed Array Items
An indexed array's items are accessed by their numeric position, starting at 0 for the first element -- $fruits[0] gets the first item, $fruits[1] the second, and so on.
The last valid index in an indexed array is always one less than its total item count, since counting starts from zero rather than one.
Example: Accessing Indexed Array Items
<?php
// Declare `$fruits` as an array: `["Apple", "Banana", "Cherry"]`
$fruits = ["Apple", "Banana", "Cherry"];
// Print `$fruits[0] . "\n"` to the output
echo $fruits[0] . "\n";
// Print `$fruits[2]` to the output
echo $fruits[2];
?>
Login to try C/C++/Java/PHP code in the editor
Accessing Associative Array Items
An associative array's items are accessed by their string key instead of a numeric position -- $person["name"] retrieves the value stored under the "name" key, regardless of where that key sits in the array.
Example: Accessing Associative Array Items
<?php
// Declare `$person` as an array: `["name" => "Alice", "age" => 30]`
$person = ["name" => "Alice", "age" => 30];
// Print `$person["name"]` to the output
echo $person["name"];
?>
Login to try C/C++/Java/PHP code in the editor
Checking If a Key Exists Before Accessing It
isset($arr["key"]) returns true only if the key exists AND its value is not null, while array_key_exists("key", $arr) returns true if the key exists at all, even if its value is null -- checking first avoids the warning that comes from accessing a genuinely missing key.
Example: Checking If a Key Exists Before Accessing It
<?php
// Declare `$arr` as an array: `["name" => "Alice", "email" => null]`
$arr = ["name" => "Alice", "email" => null];
// Print a detailed dump (with types) of `isset($arr["email"])`
var_dump(isset($arr["email"]));
// Print a detailed dump (with types) of `array_key_exists("email", $arr)`
var_dump(array_key_exists("email", $arr));
?>
Login to try C/C++/Java/PHP code in the editor
Safe Access with the Null Coalescing Operator
$arr["key"] ?? $default reads a value if the key exists and is not null, or returns $default otherwise -- all in one expression, without needing a separate isset() check or triggering an undefined-key warning.
Example: Safe Access with the Null Coalescing Operator
<?php
// Declare `$arr` as an array: `["name" => "Alice"]`
$arr = ["name" => "Alice"];
// Print `$arr["email"] ?? "no email set"` to the output
echo $arr["email"] ?? "no email set";
?>
Login to try C/C++/Java/PHP code in the editor
Accessing Nested Array Items
When an array contains other arrays (a multidimensional array), chain multiple square brackets to reach a deeply nested value -- $data["user"]["address"]["city"] walks through three levels of nested arrays to reach the final value.
Example: Accessing Nested Array Items
<?php
// Declare `$data` as an array: `["user" => ["address" => ["city" => "Austin"]]]`
$data = ["user" => ["address" => ["city" => "Austin"]]];
// Print `$data["user"]["address"]["city"]` to the output
echo $data["user"]["address"]["city"];
?>
Login to try C/C++/Java/PHP code in the editor
- Accessing an index or key that does not exist, which triggers an "undefined array key" warning and returns null instead of a real value.
- Forgetting that indexed array positions start at 0, not 1 -- the first element is $arr[0], not $arr[1].
- Using a numeric string key like "5" and a true integer key 5 interchangeably without realizing PHP treats numeric string keys as integers internally.
- Square bracket syntax, $arr[index] or $arr["key"], reads the value stored at that position or key.
- Accessing a nonexistent key returns null and raises a warning; isset() or array_key_exists() can check safely first.
- The null coalescing operator, $arr["key"] ?? $default, safely reads a possibly-missing key with a fallback in one line.
Array access syntax has been part of PHP since its earliest versions and works identically for both indexed and associative arrays.
Chapter Quiz — Complete all 13 topics to unlock
0/13 topics done
Complete these topics first: