1

I've tried multiple ways without success, is there anyway I can organise the below so the result is alphabetical?

<?php
    $datas = array_slice($facilities, 0, $leftoshownumber);
    foreach($datas as $data) {
        if(in_array ($data->Name,$removedouble)) continue; 
        echo '<div class="checkmark-33"><div class="fa-stack fa-1x checkmark-icon"><i class="fa fa-circle fa-stack-2x icon-background"></i><i class="fa fa-check fa-stack-1x icon-text"></i></div><div class="checkmark-inner">'. $data->Name .'</div></div>'; 
    }; 
?> 

Any help would be great, Thanks!

fusion3k
  • 11,568
  • 4
  • 25
  • 47
cameronmarklewis
  • 266
  • 1
  • 3
  • 15

2 Answers2

1

In your code, $datas is an array of objects, with each individual $data object containing a "Name" field. So you need to sort the $datas array alphabetically based on the value of each $data->Name field. As explained in the question Sort array of objects by object fields, this can be done with usort:

<?php
    function cmp($a, $b) {
        return strcmp($a->Name, $b->Name);
    }

    $datas = array_slice($facilities, 0, $leftoshownumber);
    usort($datas, "cmp");

    foreach($datas as $data) {
        if(in_array ($data->Name,$removedouble)) continue; 
            echo '<div class="checkmark-33"><div class="fa-stack fa-1x checkmark-icon"><i class="fa fa-circle fa-stack-2x icon-background"></i><i class="fa fa-check fa-stack-1x icon-text"></i></div><div class="checkmark-inner">'. $data->Name .'</div></div>'; 
    }; 
?> 
Community
  • 1
  • 1
0

You could use a user defined sort, usort:

<?php 
    function cmp($a, $b){ // User defined sorting algorithm
        if ($a == $b)
            return 0;
        return ($a['name'] < $b['name']) ? -1 : 1;
    }
    $datas = array_slice($facilities, 0, $leftoshownumber);
    usort($datas, "cmp"); // use the function cmp() to do sorting comparison 
    foreach($datas as $data) {
        if(in_array ($data->Name,$removedouble)) continue; 
            echo '<div class="checkmark-33"><div class="fa-stack fa-1x checkmark-icon"><i class="fa fa-circle fa-stack-2x icon-background"></i><i class="fa fa-check fa-stack-1x icon-text"></i></div><div class="checkmark-inner">'. $data->Name .'</div></div>'; 
    }; 

?> 
Chizzle
  • 1,707
  • 1
  • 17
  • 26