I am trying to sort an array inside foreach. Following is my code:
<?php $_images = Mage::getModel('catalog/product')->load($_product->getId())->getMediaGalleryImages(); ?>
<?php $i = 0; foreach ($_images as $_image){ $i++; ?>
<img src="<?php echo $this->helper('catalog/image')->init($_product, 'thumbnail', $_image->getFile())->resize(300,300); ?>" style="width:50px;height:50px;">
<?php } ?>
I need the images to be printed out in the correct order. It should be ordered by the value of:
$_image[position_default]
I have tried using Ksort:
<?php $_images = Mage::getModel('catalog/product')->load($_product->getId())->getMediaGalleryImages(); ?>
<?php $i = 0; foreach ($_images as $_image){ $i++; ?>
<img src="<?php echo $this->helper('catalog/image')->init($_product, 'thumbnail', $_image->getFile())->resize(300,300); ?>" style="width:50px;height:50px;">
<?php ksort($_image['position']); ?>
<?php } ?>
But it does not sort them at all. Maybe i don't use it correctly? Any suggestions? :)
Some output of $_image:
[224] => Varien_Object Object
(
[_data:protected] => Array
(
[value_id] => 224
[file] => /s/a/salty_fred_kn_kket.jpg
[label] =>
[position] => 2
[disabled] => 0
[label_default] =>
[position_default] => 2
[disabled_default] => 0
[url] => http://webshop.simplychocolate.dk/media/catalog/product/s/a/salty_fred_kn_kket.jpg
[id] => 224
[path] => /home/www/webshop.simplychocolate.dk/media/catalog/product/s/a/salty_fred_kn_kket.jpg
)
[_hasDataChanges:protected] =>
[_origData:protected] =>
[_idFieldName:protected] =>
[_isDeleted:protected] =>
[_oldFieldsMap:protected] => Array
(
)
[_syncFieldsMap:protected] => Array
(
)
)
[247] => Varien_Object Object
(
[_data:protected] => Array
(
[value_id] => 247
[file] => /i/m/image_150.jpg
[label] =>
[position] => 3
[disabled] => 0
[label_default] =>
[position_default] => 1
[disabled_default] => 0
[url] => http://webshop.simplychocolate.dk/media/catalog/product/i/m/image_150.jpg
[id] => 247
[path] => /home/www/webshop.simplychocolate.dk/media/catalog/product/i/m/image_150.jpg
)
[_hasDataChanges:protected] =>
[_origData:protected] =>
[_idFieldName:protected] =>
[_isDeleted:protected] =>
[_oldFieldsMap:protected] => Array
(
)
[_syncFieldsMap:protected] => Array
(
)
)
[258] => Varien_Object Object
(
[_data:protected] => Array
(
[value_id] => 258
[file] => /a/v/avatar.png
[label] =>
[position] => 3
[disabled] => 0
[label_default] =>
[position_default] => 3
[disabled_default] => 0
[url] => http://webshop.simplychocolate.dk/media/catalog/product/a/v/avatar.png
[id] => 258
[path] => /home/www/webshop.simplychocolate.dk/media/catalog/product/a/v/avatar.png
)
[_hasDataChanges:protected] =>
[_origData:protected] =>
[_idFieldName:protected] =>
[_isDeleted:protected] =>
[_oldFieldsMap:protected] => Array
(
)
[_syncFieldsMap:protected] => Array
(
)
)
foreachloop.$_image['position']key or the$_images[key]key?