1

我将这些数据存储在一个数组中$eventTitles。我正在尝试按字母顺序对其进行排序。

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
) 

使用:

$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>';
}

当我尝试遍历标题并将它们逐个呈现时会产生以下错误:

警告:在第 281 行的 model.php 中为 foreach() 提供的参数无效

任何关于出了什么问题的线索我都会非常感激。

4

1 回答 1

3

ksort接受一个数组引用并返回一个布尔值truefalse)。

当你这样做时,$eventTitles = ksort($eventTitles);你正在覆盖$eventTitles一个替换数组的布尔值。

做就是了:

ksort($eventTitles);

上的文档ksort(...)

于 2012-11-07T18:54:49.987 回答