How can I get collection result "column headers" or "column names" from query?

Suppose I have $data = Mage::getModel('catalog/product')->getCollection(); and I have some joins with other tables

I can get the data by running foreach on $data giving me the actual output data.

question: but how can I get the "header name" (or "column name") as I would see it when I run the same query in MySQL

enter image description here

  • When referring to $data you mean you need to get all array keys within that? Jan 15, 2018 at 19:09
  • I am looking to get the column name (the data header value) not the data within the data matrix itself
    – snh_nl
    Jan 15, 2018 at 19:43

2 Answers 2


For a simple query like SELECT * FROM 'sales_flat_order'

$res    = Mage::getSingleton('core/resource');
$read   = $res->getConnection('core_read');
$table  = $res->getTableName('sales/order');
$header = array_keys($read->describeTable($table));

To get all product attributes from Mage::getModel('catalog/product')->getCollection() just use keys from $item->getData().

  • It is a collection with joins etc
    – snh_nl
    Jan 15, 2018 at 19:41
  • @snh_nl whats wrong with getData()?
    – sv3n
    Jan 15, 2018 at 19:44
  • So on first for each iteration grab keys?
    – snh_nl
    Jan 15, 2018 at 19:46
  • @snh_nl Yep. I'd do so.
    – sv3n
    Jan 15, 2018 at 19:56

To get the collection's columns, consider the following.

<?php $collection = $this->getCollection() ?>
<?php $columns    = $this->getColumns(); ?>

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge that you have read and understand our privacy policy and code of conduct.

Not the answer you're looking for? Browse other questions tagged or ask your own question.