explode textarea and commas Am attempting to recreate a common feature, where a user can type several items in to a textarea, separated by commas and newlines and then submit them, i.e. for multiple record entry.

The data does not come from a CSV file and is manually entered. 

// data looks like this in the textarea:

Widget 1, 4, 10
Widget 2, 1.3, 10
Widget 3, 5.5, 15


Then submit the form:

$products = explode("\r\n", $_POST['productsTextarea']); // sanitize

$y = 0; 
$productArray = array();

// explode and trim each product, assign values to an array
foreach($products as $product) :      
    $product = array_map('trim', explode(',', $product));
    $productArray[$y]['name'] = $product [0];
    $productArray[$y]['cost'] = $product [1];
    $productArray[$y]['quantity'] = $product [2];
    $y++;
endforeach;


-

// desired output of $productArray
array(
    (int) 0 =&gt; array(
        'name' =&gt; 'Widget 1',
        'cost' =&gt; '4',
        'quantity' =&gt; '10',
    ),
    (int) 1 =&gt; array(
        'name' =&gt; 'Widget 2',
        'cost' =&gt; '1.3',
        'quantity' =&gt; '10',
    ),
    (int) 2 =&gt; array(
        'name' =&gt; 'Widget 3',
        'cost' =&gt; '5.5',
        'quantity' =&gt; '15',
    ),
)


I feel this isn't particularly extensible (i.e. the array keys are hard coded) and wonder how it would cope with a lot of items. 

Name, Cost, Quantity, Colour, Location, Postcode // header row?
Widget 1, 4, 10, blue, Foo, Bar
Widget 2, 4, 10, red, Foo, Bar
Abstract class is ideal for this:

abstract class ProductsTextareaBase {

  protected abstract function getColumns();

  public function parse($productsTextarea) {
    $products = explode("\r\n", $productsTextarea); // sanitize

    $y = 0; 
    $productArray = array();

    // explode and trim each product, assign values to an array
    foreach($products as $product) {      
        $product = array_map('trim', explode(',', $product));
        $i = 0;
        $columns = $this-&gt;getColumns();
        foreach($product as $col) {
          $productArray[$y][$columns[$i]] = $col;
          $i++;
        }
        $y++;
    }
    return $productArray;
  }
}

class WidgetProductsTextarea extends ProductsTextareaBase {
  protected function getColumns() {
    return array('name', 'cost', 'quantity');
  }
}