How to get single value from this multi-dimensional PHP array

How to get single value from this multi-dimensional PHP array

  1. Blog
  2. 15 November 2026
  3. 3 min read

Stefan Bogdanescu

Founder & Senior Architect

How to get single value from this multi-dimensional PHP array

You have a variable containing a nested structure, and you need to access a specific property like email. The core issue isn't about "flattening" the array or changing its data type; it is about correctly navigating the index hierarchy. In your example, $myarray is not a flat list of records; it is a single element inside an outer array at index [0].

To retrieve email@example.com, you must chain the indices. The syntax requires accessing the first level ($myarray[0]) before accessing the properties of that inner associative array. Attempting to run $myarray['email'] directly will fail with a notice or undefined index error because the root key is numeric, not a string named 'email'.

Here is how you resolve it in standard procedural PHP:

$email = $myarray[0]['email'];
echo $email; // Outputs: email@example.com

This approach assumes the structure is guaranteed. If your data source occasionally returns an empty outer array or if the index shifts, this direct access will throw a fatal error or return null depending on your PHP configuration.

Using Array Access Helpers

If you prefer to avoid the numeric index syntax entirely, or if you are working in a context where keys might be inconsistent, you can use helper functions that handle existence checks gracefully.

isset() Check
The isset() function is useful for verifying that the key exists before accessing it. However, it does not check if the parent array is empty; it only checks if the specific path resolves to a value.

if (isset($myarray[0]['email'])) {
    echo $myarray[0]['email'];
} else {
    echo "Email not found";
}

array_key_exists() Check
Unlike isset(), the array_key_exists() function returns true even if the value is null. This distinction matters if your data model allows for a null email address. If you use isset() on a key that exists but holds a null value, it will return false, potentially hiding valid data.

if (array_key_exists('email', $myarray[0])) {
    echo $myarray[0]['email']; // Works even if email is null
}

Using Array Access with Default Values

For cleaner code where you want to provide a fallback value automatically, the ?? operator (available in PHP 7+) is the modern standard. It attempts to access $myarray[0]['email'], and if that path does not exist or evaluates to false (without distinguishing between null and missing), it returns your default.

$email = $myarray[0]['email'] ?? 'unknown@example.com';
echo $email;

If you specifically need to distinguish between a missing key and a null value while still providing a default, combine the operator with a strict check:

$email = $myarray[0]['email'] ?? null;
// Then handle null explicitly if needed

The Laravel Approach

If you are running this logic within a Laravel application, you should leverage the collection system or Eloquent relationships rather than accessing raw array indices directly. Direct array manipulation often bypasses Laravel's type safety and validation layers.

Assuming this data comes from a database query using Eloquent, you likely have a model representing the user. You would typically access the attribute via the model:

$user = User::find(6578765); // Assuming ID is the primary key
$email = $user->email;       // Magic getter handles this cleanly

If you are strictly dealing with a raw JSON payload or an API response that matches your print_r output, and you cannot change the data source, you can wrap the array in a Laravel Collection to use method chaining for safe access:

use Illuminate\Support\Collection;

$collection = collect($myarray);
$email = $collection[0]['email'] ?? 'default@example.com';

This abstraction allows you to chain methods like get() which offer more readable syntax and better error handling compared to bracket notation.

Summary of Trade-offs

The choice between direct bracket access ([]) and helper functions depends on your data guarantees. If the array structure is rigid and always contains exactly one record at index [0], the bracket notation offers the highest performance with zero overhead. However, if the data is user-generated or comes from an external API where format consistency cannot be guaranteed, using isset(), array_key_exists(), or the null coalescing operator adds necessary defensive programming practices to prevent runtime errors.

Our network