I want to sort alphabetically the manufacturers images list.
I'm using this code and it generate the order that I have on magento admin


 foreach ($attribute->getOptions() as $option) {

    $src = $this->getViewFileUrl('images/' . $option->getLabel() . '.png');

 " alt="getLabel() ?>" onerror="this.onerror=null; this.remove(); " style="padding: 10px 20px;"  width="200" height="200"/>


Please help

1 Answer 1


Step - 1 create your own src image array and push the all image link in that array.

Examp - like this

$image_source_files = [];
foreach ($attribute->getOptions() as $option) {

    $src = $option->getLabel() .  '.png';
    $image_source_files[] = $src;

Step-2 $image_source_files array sort according to your requirement and check the $image_source_files array using print.

echo "Standard sorting: ";
echo "<br>";

echo "Natural order: ";

foreach ($image_source_files as $value) { echo $this->getViewFileUrl( $value) ; }

I hope this is helpful to you!!

  • Thank you for your response, can you tell me how to put the result of sorting on images src? Jul 3, 2020 at 12:40
  • I have already print the array you can check. You can just used for each for new created $image_source_files. Jul 3, 2020 at 15:00
  • i tested your code and I have as a result I have something like this:Array ( [0] => example.com/images .png [1] => https://example.com/images/...Merci.png ..... I want to put that on image src in order to display all image of eatch array element! help please Jul 3, 2020 at 15:16
  • I have upadate the answer Jul 3, 2020 at 15:49
  • but for me I want to sort images not just their names, how can I put every single array item in src of each image please? Jul 3, 2020 at 15:55

Your Answer

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

Not the answer you're looking for? Browse other questions tagged or ask your own question.