我正试图在/ categories / index
中的循环中显示类别中的关联论坛 Array
(
[0] => Array
(
[Category] => Array
(
[id] => 1
[name] => General
[status] => 1
[order] => 1
)
[Forum] => Array
(
[0] => Array
(
[id] => 1
[category_id] => 1
[name] => Lounge
[description] => Go and lounge around
[status] => 1
[order] => 1
[total_posts] => 1
[total_threads] => 1
[created] => 2009-06-04 19:13:24
)
[1] => Array
(
[id] => 2
[category_id] => 1
[name] => Test111
[description] => Test111
[status] => 1
[order] => 1
[total_posts] => 1
[total_threads] => 1
[created] => 2009-06-04 19:16:26
)
)
)
然而,为了让论坛显示我需要设置数组值([0]),显然这在foreach循环中不起作用,我如何循环类别然后循环类别中的论坛
答案 0 :(得分:3)
如果包含所有内容的变量为$categories
:
print '<ul>';
foreach($categories as $category) {
print '<li>' . $category['Category']['name'];
if($category['Forum']) {
print '<ul>';
foreach($category['Forum'] as $forum) {
print '<li>' . $forum['name'] . '</li>';
}
print '</ul>';
}
print '</li>';
}
print '</ul>';
HTML结构只是一个例子,您可以将其更改为桌面或其他任何内容。
答案 1 :(得分:0)
$result_set=array(....);//Your main array
foreach($result_set as $category){
print_my_category_header($category['category']);
print_category_forums($category['Forum'];
}
function print_my_category_header(array $category){
//do what you need to do
}
function print_category_forums(array $forums){
foreach($forums as $forum){
echo_single_forum($forum);
}
}
function echo_single_forum(array $forum){
//echo fields in the way you want to
}