如何使用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);->正确的!非常感谢您花时间回答这个问题!