PHP how to get value from array if key is in a variable

PHP how to get value from array if key is in a variable

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

Stefan Bogdanescu

Founder & Senior Architect

PHP how to get value from array if key is in a variable

There are two common scenarios when you need to access an array element using a key stored in a variable, and confusing them leads to unexpected behavior. One involves accessing an associative array where the key is a string (e.g., 'name'), while the other involves numeric indices or keys that happen to be integers (e.g., 4). The syntax for both is similar—$array[$variable]—but understanding how PHP interprets variable interpolation in this context is critical to avoid silent failures.

When you write $value = $array[$key];, PHP looks at the value inside the brackets. If $key contains an integer, like 4, PHP treats it as a numeric index and retrieves the element at that position. This works perfectly fine for standard indexed arrays. However, if your array is associative and you expect to access a key stored in a variable (like $key = 'username'), simply using $array[$key] will still work because PHP converts the string value of $key into the appropriate offset.

The issue arises when developers confuse variable interpolation with string concatenation or attempt to use dynamic keys without ensuring the array type matches expectations. For example, if you have an associative array like ['a' => 1, 'b' => 2] and set $key = 'b', then $array[$key] returns 2. But if you try to access a key that doesn't exist, PHP returns null in loose contexts or throws an error depending on the version and configuration.

One frequent pitfall occurs when mixing up variable keys with string literals. Consider this scenario:

$keys = ['user', 'admin'];
$data = [
    'user' => 10,
    'admin' => 20
];

$key = $keys[0]; // $key is now 'user'
$value = $data[$key]; // Works fine: returns 10

This works because $key holds the string 'user', and PHP resolves it correctly. However, if you try to use a variable inside a string literal without proper interpolation—say, "$data[$key]" instead of just $data[$key]—you'll get a syntax error or incorrect behavior depending on quotes used. Always ensure you're not accidentally creating a string key when you meant to interpolate the variable.

Another common mistake involves using curly braces with parentheses:

$value = $array{($key)}; // SyntaxError in older PHP versions, valid in 8.0+

In PHP 5.x and earlier, this would be invalid syntax. In PHP 8.0+, the {} syntax allows for dynamic keys but requires careful handling to avoid confusion with static property access. Stick to $array[$key] unless you specifically need the newer syntax for readability or performance reasons in large-scale projects.

If you're working within a Laravel application, consider using helper methods like data_get() from the Illuminate\Support facade or global helper function. These provide safer access patterns with fallback values:

$value = data_get($array, $key);
// Or with a default value
$value = data_get($array, $key, 'default');

These methods are particularly useful when dealing with deeply nested arrays or optional keys. They also prevent notices or warnings if the key doesn't exist, making your code more robust in production environments.

Ultimately, the expression $value = $array[$key] is correct and widely used across all PHP versions for dynamic key access. The real challenge lies not in the syntax itself but in ensuring that $key contains the right type of value—either an integer index or a string key—that matches the structure of your array.

Our network