0

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
                        (
                        )

                )
3
  • You need to sort your array before the foreach loop. Commented Nov 21, 2013 at 15:04
  • I can't tell if you want to sort by the $_image['position'] key or the $_images[key] key? Commented Nov 21, 2013 at 15:05
  • 1
    $_image is going to be a single image. Sorting a single-element array is pointless. You need to ksort $_images, and since you're sorting a specific key, probably need to use uksort. Commented Nov 21, 2013 at 15:11

2 Answers 2

2

You will need to use a custom sorting function, because you will want to compare with the value of one of the "properties" of each image. (see http://www.php.net/manual/en/function.usort.php) So before the loop, put:

function cmp($a, $b)
{
    return strcasecmp($a['_data']['position_default'], $b['_data']['position_default']);
}

usort($_images, "cmp");

Edit: I changed from uksort to usort, made a mistake there.

Edit2: Given the output for $_images that you added, the position-default property is one level deeper, so I added the ['_data'].

Sign up to request clarification or add additional context in comments.

Comments

1

At first glance, it looks like the ksort should happen before the foreach loop.

<?php ksort($_images);  ?>
<?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 } ?>

Give that a try. Let me know how it goes!

1 Comment

Do you think you could provide a set of the data (at least the structure) returned by the Mage::getModel() call?

Your Answer

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

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.