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

如何循环处理$data?遭遇Undefined array key 'id'错误求助

问题:处理Redmine时间条目数据时出现「Undefined array key 'id'」错误

问题场景

我尝试通过循环解析Redmine API返回的时间条目数据,但现在遇到错误:Undefined array key "id",错误指向代码第50行的$id = $data['id'];。

我的代码如下:

public function handle()
{
    $redmineService = new RedmineAPIService();
    $users = $redmineService->getUsersInfo();

    foreach ($users as $user) {
        $data = $redmineService->getTimeEntriesInfo($user['id']);
        foreach ($data as $datum) {
            $id = $data['id']; // 错误行
            $project = $datum['project'];
            $activity = $datum['activity'];
            $issue = $datum['issue'];
            $comments = $datum['comments'];

            if ($this->confirm('Is this information correct?')) {
                $link = new TimeEntry();
                $link->id = $id;
                $link->project = $project;
                $link->activity = $activity;
                $link->issue = $issue;
                $link->comments = $comments;
                //dd($link->getAttributes());
                $link->save();
        }

            $this->info("Saved.");
        }
    }

    return 0;
}

执行dd($link->getAttributes());得到的输出示例:

249 => array:10 [
    "id" => 5369
    "project" => array:2 [ …2]
    "issue" => array:1 [ …1]
    "user" => array:2 [ …2]
    "activity" => array:2 [ …2]
    "hours" => 1.0
    "comments" => "Comment"
    "spent_on" => "2022-05-27"
    "created_on" => "2022-05-27T05:00:19Z"
    "updated_on" => "2022-05-27T05:00:19Z"
  ]

解决方案

  1. 修正变量名错误
    报错核心是代码误将循环单条数据的变量$datum写成了整个数据集的$data。$data是所有时间条目的集合数组,单个时间条目才是$datum,需把错误行改为:

    $id = $datum['id'];
    
  2. 优化数组字段处理(建议)
    从输出可见,project、activity、issue都是数组结构,直接存入数据库可能导致序列化问题或数据不符合预期,建议提取数组中的ID值存储:

    $projectId = $datum['project']['id'] ?? null;
    $activityId = $datum['activity']['id'] ?? null;
    $issueId = $datum['issue']['id'] ?? null;
    

    再对应赋值给TimeEntry模型的对应字段(需确保模型字段类型匹配)。

  3. 增加空值检查
    为避免API返回空数据导致的循环错误,可在遍历前增加检查:

    if (empty($data)) {
        $this->info("No time entries found for user {$user['id']}");
        continue;
    }
    

修正后的完整代码示例

public function handle()
{
    $redmineService = new RedmineAPIService();
    $users = $redmineService->getUsersInfo();

    foreach ($users as $user) {
        $data = $redmineService->getTimeEntriesInfo($user['id']);
        
        if (empty($data)) {
            $this->info("No time entries found for user {$user['id']}");
            continue;
        }

        foreach ($data as $datum) {
            $id = $datum['id'];
            $projectId = $datum['project']['id'] ?? null;
            $activityId = $datum['activity']['id'] ?? null;
            $issueId = $datum['issue']['id'] ?? null;
            $comments = $datum['comments'] ?? '';

            if ($this->confirm('Is this information correct?')) {
                $link = new TimeEntry();
                $link->id = $id;
                $link->project_id = $projectId; // 假设模型字段为project_id
                $link->activity_id = $activityId; // 假设模型字段为activity_id
                $link->issue_id = $issueId; // 假设模型字段为issue_id
                $link->comments = $comments;
                
                if ($link->save()) {
                    $this->info("Time entry {$id} saved successfully.");
                } else {
                    $this->error("Failed to save time entry {$id}: " . implode(', ', $link->getErrorMessages()));
                }
            } else {
                $this->info("Skipped saving time entry {$id}.");
            }
        }
    }

    return 0;
}

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

相关产品推荐
方舟 Agent Plan

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

最近更新时间:2026.08.21 10:06:26