You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

PHP数组数据添加问题:Volvo XC60归属错误修复求助

问题:资源归属错误的函数修复

我有一个名为getPageContent的函数,用于列出版块、子版块及其中的资源。目前输出存在问题:Volvo XC60错误归属在Vauxhall子版块下,而非Volvo子版块。我纠结的代码行如下:

$sections[$section_index]->subsections[$section_index]->subcontent[] = $subContent;

预期层级

  • Cars (Section)
    • Car Guide (Section resource)
    • Vauxhall (Sub Section)
      • Vauxhall Corsa (sub section resource)
      • Vauxhall Astra (sub section resource)
    • Volvo (Sub Section)
      • Volvo XC60 (sub section resource)

函数代码

public function getPageContent($id)
{
    $sections = $this->Pages_Section->get($id);
    $groupIds = [1,2,3,4];

    foreach ($sections as $section_index => $section) {
        for ($y = 0; $y <= 5; $y++) {
            $sections[$section_index]->content[$y] = [];
        }

        $subSections = $this->Pages_Section->order_by('position')->getwhere(['sections_parent_id' => $section->id]);
        if (is_array($subSections) && $subSections !== []) {
            foreach ($subSections as $subSection) {
                $sections[$section_index]->subsections[] = $subSection;
                $subSectionContents = $this->Pages_Sections_Resource->getbysectiongroups($subSection->id, $groupIds);
                foreach ($subSectionContents as $subIndex => $subContent) {
                    if ((int) $subContent->pages_sections_id === (int) $subSection->id) {
                       $sections[$section_index]->subsections[$section_index]->subcontent[] = $subContent;
                    }
                }
            }
        }

        $contents = $this->Pages_Sections_Resource->getbysectiongroups($section->id, $groupIds, $type);
        foreach ($contents as $content) {
            if ($content->year >= 0 && $content->year <= $programmeYear) {
                $sections[$section_index]->content[$content->year][] = $content;
            }
        }
    }

    return $sections;
}

当前$sections返回的数组

Array
(
    [0] => stdClass Object
        (
            [id] => 1273
            [pages_id] => 190
            [sections_parent_id] => 0
            [name] => Cars
            [subsections] => Array
                (
                    [0] => stdClass Object
                        (
                            [id] => 1274
                            [pages_id] => 190
                            [sections_parent_id] => 1273
                            [name] => Vauxhall
                            [subresources] => Array
                                (
                                    [0] => stdClass Object
                                        (
                                            [id] => 
                                            [pages_sections_id] => 1274
                                            [resources_id] => 28701
                                            [name] => Vauxhall Astra (resource)
                                            [year] => 0
                                            [pagesectionresource_id] => 11004
                                        )

                                    [1] => stdClass Object
                                        (
                                            [id] => 
                                            [pages_sections_id] => 1274
                                            [resources_id] => 28699
                                            [name] => Vauxhall Corsa (resource)
                                            [year] => 0
                                            [pagesectionresource_id] => 11002
                                        )

                                    [2] => stdClass Object
                                        (
                                            [id] => 
                                            [pages_sections_id] => 1275
                                            [resources_id] => 28702
                                            [name] => Volvo XC60 (resource)
                                            [year] => 0
                                            [pagesectionresource_id] => 11005
                                        )

                                )

                        )

                    [1] => stdClass Object
                        (
                            [id] => 1275
                            [pages_id] => 190
                            [sections_parent_id] => 1273
                            [name] => Volvo
                        )

                )

        )

)

内容的提问来源于stack exchange,提问作者Jake

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.07.09 10:43:19