Access Array Items

Ma Mahalakshmi V Updated 14 Nov 2026
6 min read ·Lesson 81 of 255

How Array Access Works

In simple terms: To read a value from an array, you write the array's name followed by the item's key in square brackets: $fruits[0] or $user['email'].

Technically: The array access operator [] looks up a key in the array's hash table and returns the associated value. For nested arrays, each additional pair of brackets looks up a key in the result of the previous lookup. Accessing a key that does not exist returns null and raises a warning.


Accessing by Index and by Key

<?php
$fruits = ['Mango', 'Banana', 'Guava'];
$user = ['name' => 'Asha', 'email' => 'asha@example.com'];

echo $fruits[0] . "\n";
echo $fruits[2] . "\n";
echo $user['email'] . "\n";

Output

Mango
Guava
asha@example.com
  • Indexed arrays use integer positions starting at 0.
  • Associative arrays use their string keys. Keys are case-sensitive: $user['Email'] is a different (missing) key.

Using Variables and Expressions as Keys

<?php
$prices = ['small' => 99, 'medium' => 149, 'large' => 199];

$selectedSize = 'medium';
echo $prices[$selectedSize] . "\n";

$fruits = ['Mango', 'Banana', 'Guava'];
$position = 1;
echo $fruits[$position + 1] . "\n";

Output

149
Guava

The key can be any expression: a variable, a calculation, or a function result. This is how code reads values chosen at runtime, such as the size a user selected.


Accessing Nested Arrays

Each pair of brackets goes one level deeper:

<?php
$order = [
    'id' => 501,
    'customer' => [
        'name' => 'Asha',
        'address' => ['city' => 'Madurai', 'pin' => '625001'],
    ],
    'items' => [
        ['name' => 'Gel Pen', 'qty' => 10],
        ['name' => 'Notebook', 'qty' => 3],
    ],
];

echo $order['customer']['name'] . "\n";
echo $order['customer']['address']['city'] . "\n";
echo $order['items'][1]['name'] . "\n";
echo "Items in order: " . count($order['items']) . "\n";

Output

Asha
Madurai
Notebook
Items in order: 2

Reading the Path

$order['items'][1]['name'] means:

  1. $order['items'] — the list of items
  2. [1] — the second item in that list
  3. ['name'] — that item's name

Reading the brackets from left to right describes the path through the data.


Accessing Values Inside Strings

<?php
$user = ['name' => 'Asha', 'plan' => 'Pro'];
$scores = [88, 92];

echo "Welcome, {$user['name']}! Your plan: {$user['plan']}\n";
echo "First score: $scores[0]\n";

Output

Welcome, Asha! Your plan: Pro
First score: 88

Use curly braces for quoted string keys and for nested access. See String Interpolation.


Missing Keys

What Happens

<?php
$user = ['name' => 'Asha'];

$phone = $user['phone'];
var_dump($phone);

Output

PHP Warning:  Undefined array key "phone" in /path/script.php on line 4
NULL

PHP raises a warning and uses null. The script continues, which can lead to wrong output later (for example, an empty phone field in an email).

Safe Access With ??

<?php
$user = ['name' => 'Asha'];

echo $user['phone'] ?? 'Not provided';
echo "\n";
echo $user['address']['city'] ?? 'City unknown';
echo "\n";

Output

Not provided
City unknown

?? checks the whole path without warnings, even when an intermediate level (address) is missing. It is the standard way to read optional data.

Checking First

Checktrue When
isset($array['key'])The key exists and its value is not null
array_key_exists('key', $array)The key exists (value may be null)
!empty($array['key'])The key exists and its value is truthy
<?php
$form = ['name' => 'Asha', 'nickname' => null, 'age' => '0'];

var_dump(isset($form['nickname']));
var_dump(array_key_exists('nickname', $form));
var_dump(!empty($form['age']));

Output

bool(false)
bool(true)
bool(false)

Note the empty() trap: '0' counts as empty, even though an age of zero could be a real answer.

Accessing Keys on Non-Arrays

<?php
$user = null;   // for example, a database lookup that found nothing

echo $user['name'];

Output

PHP Warning:  Trying to access array offset on value of type null in /path/script.php on line 4

Functions that "usually" return an array may return null or false when nothing is found. Check the result first, or use $user['name'] ?? 'Guest', which handles a null array as well.


First and Last Elements

<?php
$queue = ['ORD-11', 'ORD-12', 'ORD-13'];
$prices = ['pen' => 20, 'book' => 250, 'bag' => 1299];

echo $queue[0] . "\n";
echo $queue[array_key_last($queue)] . "\n";

echo array_key_first($prices) . "\n";
echo $prices[array_key_last($prices)] . "\n";

Output

ORD-11
ORD-13
pen
1299
FunctionReturnsAvailable Since
array_key_first($array)The first key (null if empty)PHP 7.3
array_key_last($array)The last key (null if empty)PHP 7.3
array_first($array)The first value (null if empty)PHP 8.5
array_last($array)The last value (null if empty)PHP 8.5
reset($array) / end($array)First / last value, but moves the internal pointerOld API

array_key_first() and array_key_last() work for any keys, including associative arrays and lists with gaps, unlike $array[0] or $array[count($array) - 1].


Extracting One Field From Records

array_column() reads the same key from every record:

<?php
$students = [
    ['id' => 11, 'name' => 'Asha', 'score' => 88],
    ['id' => 12, 'name' => 'Bala', 'score' => 72],
    ['id' => 13, 'name' => 'Charu', 'score' => 95],
];

echo implode(', ', array_column($students, 'name')) . "\n";

$scoresById = array_column($students, 'score', 'id');
echo $scoresById[13] . "\n";

Output

Asha, Bala, Charu
95
  • array_column($records, 'name') returns a list of names.
  • With a third argument, array_column($records, 'score', 'id') uses one field as the key and another as the value — a quick way to build a lookup table.

Practical Example: Reading Nested Data by Path

APIs often return deeply nested data where some fields may be missing. A small helper can read a value by a dot-separated path and fall back to a default:

<?php
function arrayGet(array $data, string $path, mixed $default = null): mixed
{
    foreach (explode('.', $path) as $segment) {
        if (!is_array($data) || !array_key_exists($segment, $data)) {
            return $default;
        }

        $data = $data[$segment];
    }

    return $data;
}

$order = [
    'id' => 501,
    'customer' => ['name' => 'Asha', 'address' => ['city' => 'Madurai']],
    'items' => [['name' => 'Gel Pen', 'qty' => 10], ['name' => 'Notebook', 'qty' => 3]],
];

var_dump(arrayGet($order, 'customer.address.city'));
var_dump(arrayGet($order, 'items.1.qty'));
var_dump(arrayGet($order, 'customer.phone', 'N/A'));
var_dump(arrayGet($order, 'id.extra', 'N/A'));

Output

string(7) "Madurai"
int(3)
string(3) "N/A"
string(3) "N/A"

Step-by-Step Explanation

  1. The path is split into segments: 'items.1.qty' becomes ['items', '1', 'qty'].
  2. For each segment, the function checks that the current value is an array and contains the key, then moves one level deeper.
  3. If any step fails, it returns the default instead of raising a warning.
  4. The segment '1' is a string, but PHP treats the integer-like string key '1' and the integer key 1 as the same key, so array_key_exists('1', ...) finds the second item.
  5. 'id.extra' stops safely because 501 is not an array.

array_key_exists() is used instead of isset() so that keys whose value is null are returned as null, not replaced by the default. Laravel provides the same idea as the helper data_get().


Common Mistakes

Off-by-One Indexes

Effect: $list[count($list)] reads past the end.

Fix: The last index is count($list) - 1, or use array_key_last().

Wrong Key Case or Spelling

Effect: $user['Email'] is undefined when the key is 'email'.

Fix: Use consistent key names, ideally defined in one place.

Reading Optional Keys Directly

Effect: Warnings and null values spreading through the code.

Fix: Use ?? with a sensible default.

Accessing Results Without Checking Them

Effect: "Trying to access array offset on value of type null/bool".

Fix: Check the result (if ($row === null)) or use ??.

Using empty() to Test for Presence

Effect: Valid values like 0 or '0' are treated as missing.

Fix: Use isset(), array_key_exists(), or explicit comparisons.


Best Practices

  • Read required keys directly and optional keys with ??.
  • Use array_key_exists() when null is a meaningful value.
  • Use array_key_first() / array_key_last() (or array_first() / array_last() in PHP 8.5+) instead of index arithmetic.
  • Use array_column() to extract fields from lists of records.
  • Validate data from outside sources before relying on its structure.

Official reference: https://www.php.net/manual/en/language.types.array.php#language.types.array.syntax.accessing

0 Comments

Reviewed before they appear

No comments yet.

PHP
PHP Home PHP Intro PHP Install PHP Built-in Web Server PHP Syntax PHP Tags PHP and HTML PHP Comments PHP Multiline Comments PHP Variables Variables Scope Static Variables Variable Variables PHP Echo / Print PHP printf / sprintf PHP Data Types PHP Booleans PHP NULL PHP Strings String Functions Modify Strings Concatenate Strings Slicing Strings Escape Characters String Interpolation Heredoc and Nowdoc Search in Strings Multibyte Strings PHP Floats PHP Casting PHP Type Juggling PHP Math PHP Random Numbers PHP Constants PHP Magic Constants PHP Operators Arithmetic Operators Assignment Operators Comparison Operators Increment / Decrement Operators Logical Operators Null Coalescing Operator Spaceship Operator Operator Precedence PHP If...Else...Elseif PHP If PHP If Operators PHP If...Else PHP Shorthand if PHP Nested if PHP Switch PHP Match Alternative Syntax PHP Loops While Loop Do While Loop For Loop Foreach Loop Nested Loops Break Statement Continue Statement PHP Functions User-defined Functions Function Arguments Default Argument Values Pass by Reference Named Arguments Variadic Functions Return Values Type Declarations in Functions Recursive Functions Variable Functions Anonymous Functions Closures Arrow Functions First-class Callable Syntax PHP Arrays Indexed Arrays Associative Arrays Create Arrays Access Array Items Update Array Items Add Array Items Remove Array Items Sorting Arrays Multidimensional Arrays Array Destructuring Array Spread Operator Search in Arrays Merge Arrays array_map / array_filter / array_reduce Array Functions PHP Superglobals $GLOBALS $_SERVER $_REQUEST $_POST $_GET $_FILES $_COOKIE $_SESSION \$\_ENV PHP RegEx RegEx Patterns RegEx Modifiers preg_match preg_replace preg_split PHP RegEx Functions PHP Form Handling PHP Form Validation PHP Form Required PHP Form URL/E-mail Form Input Sanitizing Escaping Output (htmlspecialchars) Checkbox, Radio and Select Inputs Sticky Forms PHP Form Complete PHP Date and Time Date Formatting DateTime Class Timezones PHP Include Include vs Require PHP File Handling PHP File Open/Read PHP File Create/Write File Delete / Rename / Copy PHP Directories PHP CSV Files PHP File Upload PHP Cookies PHP Sessions PHP Filters PHP Filters Advanced PHP Callback Functions PHP JSON PHP Exceptions Error Handling Custom Error Handler Error Logging HTTP Headers and Redirects Output Buffering Sending Email PHP cURL Consuming REST APIs PHP Generators PHP What is OOP PHP Classes/Objects The $this Keyword PHP Properties PHP Methods PHP Constructor Constructor Property Promotion PHP Destructor PHP Access Modifiers Readonly Properties PHP Inheritance Method Overriding The final Keyword PHP Class Constants PHP Abstract Classes PHP Interfaces Polymorphism PHP Traits PHP Static Methods PHP Static Properties Late Static Binding Magic Methods Object Cloning Comparing Objects Anonymous Classes PHP Enums PHP Namespaces Autoloading Classes PHP Iterables Iterators Object Serialization Strict Types Union Types Nullable Types Mixed and Never Types Nullsafe Operator PHP Attributes PHP Fibers Readonly Classes Property Hooks (PHP 8.4) Asymmetric Visibility (PHP 8.4) Reflection API SPL Data Structures MySQL Database MySQL Connect MySQL Create DB MySQL Create Table MySQL Insert Data MySQL Get Last ID MySQL Insert Multiple MySQL Prepared MySQL Select Data MySQL Where MySQL Order By MySQL Delete Data MySQL Update Data MySQL Limit Data MySQL Joins with PHP Pagination with PHP PDO Introduction PDO Connect PDO Prepared Statements PDO Fetch Modes PDO Transactions PDO Error Handling PHP XML Parsers PHP SimpleXML Parser PHP SimpleXML - Get PHP XML Expat Parser PHP DOM Parser PHP XMLReader PHP XMLWriter AJAX Intro AJAX PHP AJAX Database AJAX XML AJAX Live Search AJAX Poll AJAX with JSON Security Introduction Password Hashing SQL Injection Prevention XSS Prevention CSRF Protection Session Security Secure Cookies File Upload Security Filesystem Security Error Reporting in Production Hiding PHP Encryption with Sodium PHP Configuration (php.ini) PHP Command Line (CLI) Composer PSR-4 Autoloading Installing Extensions (PECL / PIE) Debugging PHP OPcache & Performance Garbage Collection PHP-FPM Migrating to PHP 8 Project: Contact Form with Email Project: Login & Registration System Project: CRUD App with MySQL Project: Image Upload Gallery Project: Shopping Cart with Sessions Project: Simple Blog Project: REST API with PHP
Ask about this post
AI Ask about this post

Ask questions about Access Array Items and get answers drawn from it.

Signed-in readers only.