Note that there are some explanatory texts on larger screens.

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

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload