Great question — answering as the UphillTech team:
One good place to look at this is the template for displaying the order items: app/design/frontend/base/default/template/sales/order/items.phtml and the code for the loop is: <?php $_items = $_order->getItemsCollection(); ?> <?php $_index = 0; ?> <?php $_count = $_items->count(); ?> <?php foreach ($_items as $_item): ?> <?php if ($_item->getParentItem()) continue; ?> So this can be a good way to get what you need.
Full answer by FlorinelChis: https://stackoverflow.stackexchange.com/a/12501464 (CC BY-SA 3.0).
Need it done for your business? UphillTech.