Warning: file_get_contents(/data/phpspider/zhask/data//catemap/5/ruby-on-rails-4/2.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
如何使用php按字母顺序对该数组进行排序,并使用foreach对其进行循环_Php_Arrays_Foreach_Ksort - Fatal编程技术网

如何使用php按字母顺序对该数组进行排序,并使用foreach对其进行循环

如何使用php按字母顺序对该数组进行排序,并使用foreach对其进行循环,php,arrays,foreach,ksort,Php,Arrays,Foreach,Ksort,我将这些数据存储在一个数组中,存储为$eventTitles。我正试着按字母顺序排序 Array ( [Customer Challenge - Sustainability] => Customer Challenge - Sustainability [Manifesto Stores] => Manifesto Stores [Helpful Heroes] => Helpful Heroes [Ben 5 places left test

我将这些数据存储在一个数组中,存储为
$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>';
}
$eventTitles=ksort($eventTitles);
foreach($eventTitles作为$title){
$t=urlencode($title);
//if语句检查标题是否在url参数中
//如果是的话,我们可以把选中的作为一个类放在左边的导航中
如果($_GET[“title”]==$title){
$selected='class=“selected”';
}否则{
$selected='';
}
$rtnStr.='
  • '; }
    当我尝试循环浏览标题并渲染每个标题时,会产生以下错误:

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


    任何关于出错原因的线索都将不胜感激。

    ksort
    获取数组引用并返回一个
    true
    false

    当您执行以下操作时,
    $eventTitles=ksort($eventTitles)
    您正在将
    $eventTitles
    覆盖为一个布尔值,以替换数组

    只要做:

    ksort($eventTitles);
    

    请格式化数组,使其更具可读性。您需要数组和字符串…您不需要为ksort分配变量。只需执行ksort(..)$eventTitles=ksort($eventTitles);->假的!ksort($eventTitles);->正确的!非常感谢您花时间回答这个问题!