-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);
4
  • 1
    try it like this $regions []= str_replace(" / ", ", ", $r)"; Commented May 14, 2014 at 9:01
  • Use an array instead of a string...!? Commented May 14, 2014 at 9:01
  • 3
    Have you tried pushing the regions into an array, sort that one and than each again to create a string? Commented May 14, 2014 at 9:01
  • 2
    possible duplicate of PHP: How to sort the characters in a string? Commented May 14, 2014 at 9:02

1 Answer 1

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);
Sign up to request clarification or add additional context in comments.

Comments

Your Answer

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

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.