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

LINE BOT FLEX开发中foreach()遍历数据仅显示单条记录问题求助

错误排查

你的代码仅能展示单条记录由3处逻辑错误导致:

  • $itemindeks变量定义在foreach循环内部,每轮循环都会覆盖上一轮生成的数据,循环结束后变量中仅保留最后一条遍历结果
  • 索引计数变量$arraynumber写在循环内部,每轮循环都会被重置为0,即使改为追加逻辑也会出现数组索引覆盖
  • LINE FLEX消息的body外层垂直容器不应该在循环中重复创建,需要提前在循环外初始化,循环逻辑仅负责生成单条列表项追加到容器的内容数组中
修正后代码
function indexflex($title,$label,$text){
    $data = $this->Dbs->getdata_kamusengineer()->result();
    // 提前初始化body外层容器,移到循环外
    $itemindeks = array (
        'type' => 'box',
        'layout' => 'vertical',
        'contents' => array ()
    );
    // 计数器移到循环外初始化
    $arraynumber = 0;

    foreach ($data as $d) {
        $dariindo=$d->dariindo;
        $bajep=$d->bajep;

        // 仅生成单条列表项,追加到contents数组中
        $itemindeks['contents'][$arraynumber++] = array (
            'type' => 'box',
            'layout' => 'horizontal',
            'contents' => array (
                0 => array (
                    'type' => 'text',
                    'text' => $dariindo,
                    'align' => 'center',
                    'wrap' => true,
                    'gravity' => 'center',
                    'contents' => array (),
                ),
                1 => array (
                    'type' => 'separator',
                    'margin' => 'sm',
                    'color' => '#000000FF',
                ),
                2 => array (
                    'type' => 'text',
                    'text' => $bajep,
                    'align' => 'center',
                    'wrap' => true,
                    'gravity' => 'center',
                    'contents' => array (),
                ),
            ),
        );
    }
    
    $item = array (
        'type' => 'bubble',
        'direction' => 'ltr',
        'header' => array (
            'type' => 'box',
            'layout' => 'vertical',
            'contents' => array (
                0 => array (
                    'type' => 'text',
                    'text' => $title,
                    'weight' => 'bold',
                    'align' => 'center',
                    'wrap' => true,
                    'contents' => array (),
                ),
            ),
        ),
        'body' => $itemindeks,
        'footer' => array (
            'type' => 'box',
            'layout' => 'horizontal',
            'contents' => array (
                0 => array (
                    'type' => 'button',
                    'action' => array (
                        'type' => 'message',
                        'label' => $label,
                        'text' => $text,
                    ),
                    'style' => 'primary',
                ),
            ),
        ),
    );

    return $item;
}

注:修正时顺手移除了原代码中重复定义的align属性冗余项,不影响原有展示逻辑。

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

相关产品推荐
方舟 Agent Plan

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

最近更新时间:2026.08.28 08:45:35