Loop a multidimensional array and only print two specific column values per row

Loop a multidimensional array and only print two specific column values per row

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

Stefan Bogdanescu

Founder & Senior Architect

Loop a multidimensional array and only print two specific column values per row

When you receive data from a database or an API, it rarely arrives in a format ready for immediate display. You often get a massive nested structure where every row contains metadata that isn't relevant to the current view. In this scenario, you have a multidimensional array where each index represents a file record containing properties like fid, uid, timestamp, and more. Your goal is simple: iterate through this collection and extract only the filepath and filename for each entry.

The most direct approach in PHP involves using a foreach loop. This construct iterates over the outer array, giving you access to each associative array (the row) individually. Once inside the loop, you simply reference the specific keys you need.

Here is how you would implement this logic:

$files = [
    [
        'filepath' => 'sites/default/files/trucks_10785.jpg',
        'filename' => 'trucks_10785.jpg',
        // ... other keys
    ],
    [
        'filepath' => 'sites/default/files/school.jpg',
        'filename' => 'school.jpg',
        // ... other keys
    ],
    // ... more rows
];

foreach ($files as $file) {
    echo "Filepath: {$file['filepath']}<br>";
    echo "Filename: {$file['filename']}<br><hr>";
}

This code snippet assumes that every row in the array is guaranteed to contain both keys. While this works perfectly for valid data, it introduces a risk if the source data changes. If a specific file record is missing the filepath key, accessing $file['filepath'] will result in an empty string or, depending on your error handling settings, a notice.

A more robust implementation uses null coalescing operators (??) to handle missing keys gracefully. This ensures that if a row lacks one of these columns, the output defaults to a placeholder rather than crashing or displaying unexpected behavior.

foreach ($files as $file) {
    echo "Filepath: {$file['filepath'] ?? 'No filepath provided'}<br>";
    echo "Filename: {$file['filename'] ?? 'No filename provided'}<br><hr>";
}

In many modern frameworks, particularly Laravel, this raw loop is often unnecessary. Laravel provides a helper method called dd (dump and die) for debugging, but for production output or building views, the Blade template engine offers a cleaner syntax. If you are passing this $files variable to a Blade view, you can use the @foreach directive combined with array shortening syntax.

@foreach($files as $file)
    <p>Filepath: {{ old('filepath', $file['filepath']) }}</p>
    <p>Filename: {{ old('filename', $file['filename']) }}</p>
@endforeach

The old() helper is useful here to maintain state if the user submitted a form that failed validation, though in this specific read-only context, direct access is sufficient. The key takeaway is that while PHP arrays are flexible, you must decide how strictly you want to handle missing data. Strict checking (isset()) makes your code verbose but explicit about expectations, while loose checking allows for cleaner reads at the cost of potential runtime issues if data integrity isn't enforced upstream.

If you are working in a Laravel project and this array comes from a model query or a resource controller, consider converting that collection into a Resource class. This allows you to define exactly which attributes should be exposed to the frontend. By mapping only filepath and filename in your toArray() method, you ensure that even if the underlying database adds new columns later, your view remains focused on the necessary data without requiring code changes.

Our network