-4

I am adding the names of regions to a a variable using the below code (shortened). Everything works as intended, except for the sort function which throws an error saying that it requires an array instead of a string.

How can I still manage to sort the content of my variable alphabetically ?

$regions = '';
$countR = 1;
foreach ($objR->days as $days) {
    if($days->dateMatch == "Yes" && !empty($days->regions)) {
        foreach(explode(',', $days->regions) as $r){
            $regions .= str_replace(" / ", ", ", $r)) . "<br />";
            $countR++;
        }
    }
}
sort($regions);
halfer
  • 19,824
  • 17
  • 99
  • 186
user2571510
  • 11,167
  • 39
  • 92
  • 138

1 Answers1

1

Try this: You should use array for storage.

$regions = array();
$countR = 1;
foreach ($objR->days as $days) {
    if($days->dateMatch == "Yes" && !empty($days->regions)) {
        foreach(explode(',', $days->regions) as $r){
            $region = str_replace(" / ", ", ", $r)) . "<br />";
            array_push($regions,$region);
            $countR++;
        }
    }
}
sort($regions);
Ajitha Ms
  • 545
  • 5
  • 18