echo key and value of an array without and with loop

echo key and value of an array without and with loop

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

Stefan Bogdanescu

Founder & Senior Architect

Echoing Key-Value Pairs in PHP Arrays: Direct Access vs. Iteration

Working with associative arrays is a fundamental skill in PHP, yet beginners often stumble on how to extract and display specific data efficiently. Consider a simple navigation array where keys represent page titles and values point to HTML files:

$page['Home'] = 'index.html';
$page['Service'] = 'services.html';

To output Home is at index.html, you are essentially performing two distinct operations: retrieving the key and fetching its corresponding value. You have two primary approaches depending on whether you need to handle a single pair or process the entire structure.

Direct Key-Value Access

When your logic depends on a specific known key, direct access is the most readable and performant option. This method avoids the overhead of iterating through data you don't need yet. You can concatenate strings directly using double quotes in PHP, which automatically interpolates variables:

$page['Home'] = 'index.html';

echo "Home is at {$page['Home']}";
// Output: Home is at index.html

This approach relies on the assumption that the key exists. If $page['Service'] were accessed here, it would throw an error if undefined (unless strict mode isn't enforced or null coalescing is used). This is acceptable in controlled environments like view renderers where keys are guaranteed to be present. The syntax remains consistent regardless of whether the value is a string, number, or object.

Iterating Through All Elements

If you need to display every entry in the array—perhaps rendering a dynamic menu—you must iterate. PHP offers several built-in functions for this, but foreach is generally preferred for its clarity and performance compared to for loops when working with keys and values simultaneously.

foreach ($page as $key => $value) {
    echo "$key is at $value" . PHP_EOL;
}

In this snippet, $key captures the associative key (e.g., Home) and $value captures the file path (e.g., index.html). The . PHP_EOL ensures each line renders cleanly without characters bleeding into the next. Alternatively, you can use the shorthand syntax introduced in PHP 5.6:

foreach ($page as $key => $value) {
    echo "{$key} is at {$value}" . PHP_EOL;
}

This version avoids concatenation operators entirely, making the code slightly cleaner and easier to read. It produces the exact same output: Home is at index.html followed by Service is at services.html.

Performance and Readability Considerations

While modern PHP engines handle small arrays with negligible latency difference between methods, readability scales significantly with complexity. Direct access makes intent clear immediately: "I want the Home page link." Iteration signals a need to process the whole collection.

A common pitfall is using for loops with numeric indexes (for ($i = 0; $i < count($page); $i++)) on associative arrays. This approach forces you to access values via $page[$i], which retrieves the value but loses the key unless you use array_values() first, destroying the original mapping. To preserve keys during iteration, always rely on the foreach construct or current()/next() functions if you need sequential control without losing association.

For developers migrating to Laravel later in their careers, this pattern translates directly into Blade templates. You might see similar logic when rendering navigation menus:

@foreach($navItems as $key => $value)
    <li>{{ $key }} is at {{ $value }}</li>
@endforeach

Understanding these core mechanisms ensures you write PHP that is both efficient and maintainable, avoiding unnecessary overhead while keeping your codebase clean.

Our network