PHP Access Array Items
In this page:
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.
Note: Remember that array positions are zero-based; the last valid index in an array of n items is n - 1, not n.
Warning: Accessing an index beyond the array's actual length (like $fruits[10] on a 3-item array) triggers an undefined-key warning and returns null.
Example: Accessing Indexed Array Items
<?php
$fruits = ["Apple", "Banana", "Cherry"];
echo $fruits[0] . "\n";
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.
Note: Associative array keys are case-sensitive; $person["Name"] and $person["name"] refer to two entirely different keys.
Warning: A typo in a key name (like $person["nmae"]) will not raise an error at the typo itself, but will silently return null when read.
Example: Accessing Associative Array Items
<?php
$person = ["name" => "Alice", "age" => 30];
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.
Note: Use isset() for the common case of "does this key have a usable value", and array_key_exists() specifically when a null value should still count as present.
Warning: isset() returning false does not always mean the key is missing -- it also returns false if the key exists but its value is literally null.
Example: Checking If a Key Exists Before Accessing It
<?php
$arr = ["name" => "Alice", "email" => null];
var_dump(isset($arr["email"]));
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.
Note: Use ?? as the default way to read any array key that might not be present, especially for optional configuration values or user input.
Warning: Like isset(), ?? treats a genuinely-present null value the same as a missing key -- both trigger the fallback.
Example: Safe Access with the Null Coalescing Operator
<?php
$arr = ["name" => "Alice"];
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.
Note: For deeply nested, possibly-missing structures, chain ?? at each level, or check isset() on the full nested path before accessing it directly.
Warning: Accessing a nested key when an intermediate level does not exist (like $data["user"]["address"] being entirely missing) triggers a warning at that level, not just at the final key.
Example: Accessing Nested Array Items
<?php
$data = ["user" => ["address" => ["city" => "Austin"]]];
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: