1

I have this data in an array stored as $eventTitles. I am trying to sort it alphabetically.

Array (
    [Customer Challenge - Sustainability] => Customer Challenge - Sustainability
    [Manifesto Stores] => Manifesto Stores
    [Helpful Heroes] => Helpful Heroes
    [Ben 5 places left test] => Ben 5 places left test
    [Ben sold out test] => Ben sold out test
    [Ben 1 space left test] => Ben 1 space left test
    [Follow the Product] => Follow the Product
    [Living the Operating Model] => Living the Operating Model
    [Leaders Unplugged] => Leaders Unplugged
    [Market Trends] => Market Trends
    [FINAL MASTER EVENT CONFIG - DO NOT AMEND] => FINAL MASTER EVENT CONFIG - DO NOT AMEND
    [You Can Do It] => You Can Do It
    [Customer Challenge - Communicating EDLP] => Customer Challenge - Communicating EDLP
) 

Using:

$eventTitles = ksort($eventTitles);

foreach($eventTitles as $title) {
    $t = urlencode($title);
    //if statement to check if the title is in the url param 
    //and if it is we can put selected in the left hand nav as a class
    if($_GET["title"] == $title ) {
        $selected = ' class="selected"';
    } else {
        $selected = ' ';
    }
    $rtnStr .= '<li><a'.$selected.'href="list.php?title='.urlencode($title).
                       '" data-value="'.$title.'">'.$title.'</a></li>';
}

produces the following error when I try to loop through the titles and render them each out:

Warning: Invalid argument supplied for foreach() in model.php on line 281

Any clues on what is going wrong would me much appreciated.

5
  • Please format your array so that it is more readable. Commented Nov 7, 2012 at 18:52
  • you need array and have string.... Commented Nov 7, 2012 at 18:53
  • you do not need to assign a variable to ksort. Just do ksort(..) Commented Nov 7, 2012 at 18:53
  • $eventTitles = ksort($eventTitles); -> false! Commented Nov 7, 2012 at 18:54
  • ksort($eventTitles); -> right! Commented Nov 7, 2012 at 18:54

1 Answer 1

3

ksort takes an array reference and returns a boolean (true or false).

When you do $eventTitles = ksort($eventTitles); you are overwriting $eventTitles to a boolean which replaces the array.

Just do:

ksort($eventTitles);

DOCs on ksort(...)

Sign up to request clarification or add additional context in comments.

1 Comment

Thanks very much for taking the time to answer this helped!

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.