In Magento when you add configurable products into cart it would have the same sku as simple product that was actually added. Anyway every product should have its own unique SKU (or it can be generated dynamically for bundle products for example) More info can be found here
To avoid this situation you need to use getAllVisibleItems
method instead. So your code will be
foreach ($order->getAllVisibleItems() as $item) {
echo $item->getSku();
}
The difference between them is that getAllVisibleItems()
has an additional check inside the method for every item of an order !$item->getParentItemId()
It tests if the product in order has a parent product (it will give you a result without simple products for configurable, bundle etc.)