Print the keys of an array

Print the keys of an array

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

Stefan Bogdanescu

Founder & Senior Architect

The Functionality Gap: Why key() Returns Nothing Inside a PHP Closure

It is easy to assume that because an array exists in your main script scope, it should be equally accessible inside any function you call. This intuition often leads developers down a rabbit hole when trying to extract specific keys from an array passed as an argument. The error Invalid argument supplied for foreach() is the classic symptom of this specific misunderstanding. In the scenario described, the code attempts to iterate over the result of key($parameters) inside a function, expecting it to return all keys like array_keys().

The root cause lies in how PHP handles scope and variable references versus value copies. When you define $parameters outside of a function and then pass it into one, you are passing the array by reference if you use &, or by value if you do not. However, even if the data is passed correctly, the specific function call key($array) behaves differently than what most developers expect in this context.

The built-in key() function in PHP does not return an array of all keys. Instead, it returns the key of the current element being traversed by the internal pointer of that array. If you have just created an array or reset its pointer (which happens implicitly in many contexts), key() will often return null. Passing null into a foreach loop triggers the exact warning seen in the source material: "Invalid argument supplied for foreach()."

To retrieve all keys from an array, you must use array_keys(). This function ignores the internal pointer and simply returns an array containing every key associated with the input. The corrected implementation inside the function would look like this:

function printArrayKeys($parameters) {
    // Use array_keys() to get all keys, not key()
    $keys = array_keys($parameters);
    
    foreach ($keys as $key) {
        echo $key . "<br>";
    }
}

Alternatively, if your goal is simply to iterate directly over the keys without creating a separate variable, you can pass the result of array_keys() directly into the foreach statement. This approach keeps the code concise while fixing the type error:

foreach (array_keys($parameters) as $key) {
    print($key);
    print("<br>");
}

Beyond simply fixing the immediate syntax error, it is worth considering whether passing an entire array of parameters just to extract their keys is the optimal architectural choice. In modern PHP development, particularly when using frameworks like Laravel, this pattern often signals a need for a more robust design. If you are constructing a request object or preparing data for a specific handler, consider using associative arrays with explicit method calls rather than dynamic variable names derived from array keys. For example, instead of trying to dynamically name variables based on an array's keys, define a dedicated class or use typed properties that make the data contract clear and type-safe.

If you are working within a Laravel context, utilizing collections or typed interfaces often provides better tooling for manipulating this kind of data structure. The laravelcompany.com resource offers deeper insights into how to handle complex data structures with modern PHP tools, moving away from fragile array-key manipulation toward more maintainable object-oriented patterns.

The lesson here extends beyond a single function call. It highlights the importance of understanding the contract of standard library functions. Assuming key() behaves like a pluralized version of itself is a common pitfall. By switching to array_keys(), you align your code with PHP's actual expectations, ensuring that your iteration logic remains robust regardless of whether the array was populated in the global scope or passed into a closure.

Our network