Robin S Posted July 6, 2017 Posted July 6, 2017 I want to reorder my Pageimages array so that portrait-format images are placed last. Currently I'm removing portrait-format images and then adding them back. $listing_images = $page->listing_images; foreach($listing_images as $listing_image) { /* @var Pageimage $listing_image */ if($listing_image->height() > $listing_image->width()) { $listing_images->remove($listing_image); $listing_images->add($listing_image); } } This works, but just wondering if there is some better WireArray method I could use that works like "move this item to the end of the array".
LostKobrakai Posted July 6, 2017 Posted July 6, 2017 You could skip the removal with most WireArray instances, as duplications are prevented by the item keys (filename in this case). There's also insertBefore() and insertAfter() and you could use all the array sorting functions if really needed: $a = $wireArray->getArray(); sort($a) $wireArray->setArray($a); 1
kixe Posted July 6, 2017 Posted July 6, 2017 I am not sure if WireArray::sort() handles runtime vars. Try it out. foreach ($page->listing_images as $image) $image->aspectRatio = $image->width/$image->height; $page->listing_images->sort('aspectRatio'); 1
Robin S Posted July 6, 2017 Author Posted July 6, 2017 57 minutes ago, LostKobrakai said: You could skip the removal with most WireArray instances Thanks, this works well. Short and sweet so will go with this. 1 hour ago, kixe said: I am not sure if WireArray::sort() handles runtime vars. Try it out. It does work, thanks. Although in my case I don't want to change the sort order beyond moving portrait images to the end so modified it to: foreach($page->listing_images as $image) { if($image->height() > $image->width()) { $image->aspect = 2; } else { $image->aspect = 1; } } $page->listing_images->sort('aspect');
Recommended Posts
Create an account or sign in to comment
You need to be a member in order to leave a comment
Create an account
Sign up for a new account in our community. It's easy!
Register a new accountSign in
Already have an account? Sign in here.
Sign In Now