【发布时间】:2021-06-13 14:39:36
【问题描述】:
我有以下数组,我想根据某些条件更改输出
foreach($courses as $c) {
$c->type == "fulltime" ? 'full' : 'part';
$f = new stdClass();
$f->course_id = $c->course;
$f->form = $c->id;
$f->module = $c->module;
$f->unread = $unreadcount;
$f->type = $c->type;
$f->posts = $posts;
$results[$c->course_id][$c->type][] = $f;
}
当前结构:
[course_id]{
full : [
$f->form = $c->id;
$f->module = $c->module;
$f->type = $c->type; [ex: fulltime, parttime]
]
part : []
posts : []
}
条件:
if ($enableFulltimeCourse =1 && $unreadcount >0) -> 显示 full[] 和 part[] 否则为空
if ($enableParttimeCourse =1 && $f->type == "part") -> 显示部分[] 否则为空
我尝试了什么
if ($enableFulltimeCourse =1 && $unreadcount >0){
unset($f);
}
$results[$forum->course][$forum->type][] = $f;
请指教
【问题讨论】:
-
你能解释一下吗
-
@Jerson $courses 数据包含全日制和非全日制课程的结果以及基于课程类型的未读项目。通过我的前端获取值,我需要根据这些条件准备数组。例如:$enableFulltimeCourse 有一个值,未读项目不是 0,那么我想显示全职项目和帖子的子数组。否则这些子数组应该为空。我根据条件尝试了 unset($f) 但它不起作用。
-
@NicoHaase if ($enableFulltimeCourse =1 && $unreadcount >0) { unset($f);} 我试过这样但它没有用。它为每个项目返回 null。
-
你为什么要使用
unset?为什么不使用array_filter?