When running a foreach loop for an array embedded in multiple records, I can't get the array to echo just one array set per record. Instead, it's displaying all the array records, for each instance, in every result. I'm reading a CSV file, and this file can have 25 records, or 500 records (the number of records is never known), but when reading the "Options" field, the array for all the records, is being displayed in the results. Here's the code I'm running:
$option_list = explode(',', $csvdata['Options']);
foreach ($option_list as $feature) {
$output .= '<li>' . $feature . '</li>';
}
$content = '<ul>' . $output . '</ul>';
As you can see, I'm simply creating an unordered list. While I'm successful at creating the unordered list, every array in the "Options" field displays in the results as one enormous unordered list. It's not listing the single array for each specific single record.
Here's the CSV format:
Year Make Model Color Options (the array)
2010 Chevy Camaro Black 2dr, convertible, 6.2L, V8
2011 Dodge Charger Red 2dr, coupe, 3.6L, V6
2013 Ford F250 Blue 4WD, Crew Cab, V10, Tow Package
The output creates the following unordered list:
<ul>2010 Chevy Camaro Black
<li>2dr
<li>convertible
<li>6.2L
<li>V8
<li>2dr
<li>coupe
<li>3.6L
<li>V6
<li>4WD
<li>Crew Cab
<li>V10
<li>Tow Package</li></ul>
And it does this for every record. I need the unordered list to represent only the record it relates to in the CSV file, and end the loop for each record. Please let me know what I'm missing.
You're not clearing $input
before the loop, so it still contains the list items from the previous row in the CSV. Just add
$output = '';
before the foreach
statement.