How to echo out the values of this array?

How to echo out the values of this array?

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

Stefan Bogdanescu

Founder & Senior Architect

Decoding Array Iteration Logic in PHP

When you encounter an explode operation that doesn't yield the expected array structure, it usually stems from a misunderstanding of how the function populates its result set. In your specific case, the core issue lies in the variable $myarray. You are correctly using foreach ($myarray as $value), which iterates over every element in the list. However, when you attempt to access $value[0] inside that loop, you are applying array indexing logic to a scalar string value.

The explode function returns an array of strings. When you iterate with foreach, PHP assigns each string element directly to the variable $value. At this point, $value is no longer an array; it is a single string representing one part of the original date string (e.g., "2012" or "04"). Attempting to access $value[0] on a string returns only the character at the first index. Since strings are indexed by characters, $value[0] retrieves just the very first letter of that string segment. That is why you are seeing only two characters; you are effectively slicing off the first character of every item in your list rather than accessing the first item of the entire list.

To echo out the values individually as they appear in the print_r output, you need to iterate through the array elements without trying to index them further. If your goal is to process the split date components separately, simply echo $value inside the loop:

foreach ($myarray as $value) {
    echo $value . "\n";
}

This will output each segment of the date on a new line, matching the structure of Array ( [0] => 20120430 [1] => My Event 1 ) if the explode logic were applied to the full string correctly. However, looking at your source code, there is a logical gap in how $myarray is constructed. You are using explode(',', $eventDate, 2). This function splits the string at the first comma found and returns an array with exactly two elements: everything before the comma and everything after it.

If $eventDate contains "2012-05-14, My Event 3" (based on your json_decode example), explode will create an array containing "2012-05-14" and " My Event 3". Inside the loop, $value will hold these full strings. To access the specific parts, you iterate normally. If you strictly need to avoid the loop or access specific indices directly, you can do so by referencing the array keys:

$myarray = explode(',', $eventDate, 2);

// Access the date part directly
echo $myarray[0]; 

// Access the event part directly
echo $myarray[1];

The print_r output you provided shows an associative-like structure with numeric keys [0] and [1]. This confirms that $myarray is indeed a standard indexed array. The confusion often arises when developers mix up string indexing (accessing characters) with array indexing (accessing elements). Since $value inside the foreach loop represents a single element from $myarray, it must be treated as a scalar string.

If you are working within the Laravel ecosystem, ensure that your JSON data is being decoded into an associative array format if you intend to use key-based access like $item['date']. The json_decode($json_data_string, true) flag in your code correctly handles this by converting objects to arrays, which is essential for accessing properties via bracket notation. Without the true flag, $item would be an object, and $item['date'] might not behave as expected depending on the JSON structure.

Finally, consider whether you actually need to explode the string if the date components are already separated by a delimiter in the JSON payload. If the data is structured cleanly in the source JSON, parsing it directly often reduces overhead compared to splitting strings manually after extraction. Always verify the raw output of $eventDate before applying explode to ensure the delimiter matches your expectations exactly.

Our network