php foreach echo prints "Array" as value

php foreach echo prints "Array" as value

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

Stefan Bogdanescu

Founder & Senior Architect

Why foreach Prints "Array" in PHP

When you encounter a foreach loop that outputs the string "Array" instead of your expected data, the issue rarely lies with the loop syntax itself. The problem is almost always how the data was constructed prior to iteration. In the specific case provided, the code $someArray[]=array('1','2','3','4','5','6','7'); creates a single element within the array. That single element is not a scalar value like '1' or '2', but rather another array containing those values.

When the foreach loop executes, it iterates over the top-level keys of $someArray. Since there is only one key (index 0), the loop runs exactly once. Inside that iteration, the variable $value holds the entire nested array ['1', '2', '3', '4', '5', '6', '7']. When you pass an array directly to echo, PHP attempts to convert it to a string. If no explicit conversion function is called, PHP defaults to calling print_r() or similar internal logic for debugging output, which results in the string "Array" being printed.

To resolve this, you must ensure that $value contains a scalar value rather than an array during iteration. There are two primary approaches to fixing the data structure, depending on your intended use case.

Flattening the Data Structure

If your goal is simply to iterate over the numbers 1 through 7, the most direct fix is to flatten the data when assigning it. Instead of wrapping the list in an array() call and pushing that into $someArray, you should assign the elements directly or use a function that returns a flat array.

// Correct approach: Direct assignment or using array_values on a range
$numbers = ['1', '2', '3', '4', '5', '6', '7']; 

foreach ($numbers as $value) { 
    echo $value . "<br>";    
}

Alternatively, you can use array_merge to combine multiple arrays if your logic requires it, ensuring the result is a single-dimensional array before entering the loop.

Unnesting Inside the Loop

If you must maintain a multi-dimensional structure—for instance, if $someArray contains groups of data where each group has its own list of numbers—you need to handle the inner array within the loop. In this scenario, foreach will still run once per group, but you must iterate over the inner array to print the individual elements.

$someArray[] = ['1', '2', '3']; // Create a sub-array
$someArray[] = ['4', '5', '6']; // Add another sub-array

foreach ($someArray as $group) { 
    foreach ($group as $value) { 
        echo $value . "<br>";    
    } 
}

In this nested structure, the outer loop identifies each group, and the inner loop extracts the scalar values. Attempting to echo the outer $group variable directly would again result in the word "Array".

Comparison with Indexed Loops

The working example you provided uses a standard for loop with integer indexing. This works because it explicitly accesses $someArray[$i]. If your array is flat, $someArray[0] contains '1', $someArray[1] contains '2', and so on. The echo statement receives these strings directly.

However, relying on numeric indices can be fragile if the keys are sparse or non-sequential (e.g., associative arrays with string keys). The foreach construct is generally preferred because it decouples iteration from key management. It automatically retrieves the value associated with the current key, regardless of whether that key is 0, 'id', or a date string.

Best Practices for Array Iteration

When debugging array output issues, always verify the structure of your data before entering the loop. You can use var_dump() or print_r() to inspect the dimensions and keys.

var_dump($someArray); 
// Output might look like: array(1) { [0]=> array(7) { ... } }

Seeing that the first dimension has a size of 1 confirms that you are iterating over a collection of collections, not a flat list. Understanding PHP's type juggling rules—specifically how arrays convert to strings—is crucial for avoiding silent failures where your application outputs "Array" instead of meaningful data. For developers working with complex data structures in larger frameworks, ensuring proper normalization of input data before processing remains a fundamental step in maintaining clean output.

Our network