我如何使用PHP按字母顺序排序此数组,并使用foreach循环循环



我在存储为 $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行中为foreach()提供了无效的论点

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

ksort获取数组参考并返回 boolean truefalse)。

当您进行$eventTitles = ksort($eventTitles);时,您将$eventTitles覆盖为替换数组的布尔值。

只是做:

ksort($eventTitles);

ksort(...)上的文档

相关内容

  • 没有找到相关文章

最新更新