Question

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);
Was it helpful?

Solution

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);
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top