我正在尝试在foreach中对数组进行排序。 以下是我的代码:
<?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 } ?>
我需要以正确的顺序打印图像。它应按以下值排序:
$_image[position_default]
我尝试过使用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 } ?>
但它根本不对它们进行排序。也许我没有正确使用它? 有什么建议? :)
$ _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
(
)
)
答案 0 :(得分:2)
您需要使用自定义排序功能,因为您需要与每个图像的某个“属性”的值进行比较。 (见http://www.php.net/manual/en/function.usort.php) 所以在循环之前,放:
function cmp($a, $b)
{
return strcasecmp($a['_data']['position_default'], $b['_data']['position_default']);
}
usort($_images, "cmp");
编辑:我从uksort改为usort,在那里犯了错误。
Edit2:鉴于您添加的$ _images的输出,position-default
属性更深一层,所以我添加了['_data']
。
答案 1 :(得分:1)
乍一看,看起来ksort应该在foreach
循环之前发生。
<?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 } ?>
试一试。让我知道它是怎么回事!