php 使用数组Map向维度数组添加键[已关闭]

8yoxcaq7  于 2022-12-10  发布在  PHP
关注(0)|答案(1)|浏览(119)

**已关闭。**此问题需要debugging details。当前不接受答案。

编辑问题以包含desired behavior, a specific problem or error, and the shortest code necessary to reproduce the problem。这将有助于其他人回答问题。
昨天关门了。
Improve this question
我有一个维度数组,我想使用array_map添加一个名为“reschedule”的新键
这是我的阵:

$topics = [
  [
     "id" => 1,
     "statusName" => "COMPLETED",
     "lessons" => [
       [
         "id" => 1,
         "statusName" => "COMPLETED"
       ],
       [
         "id" => 1,
         "statusName" => "COMPLETED"
       ],
     ]
  ],
  [
     "id" => 2,
     "statusName" => "SCHEDULED",
     "lessons" => [
       [
         "id" => 1,
         "statusName" => "SCHEDULED"
       ],
       [
         "id" => 1,
         "statusName" => "COMPLETED"
       ],
     ]
  ]
];

我试过的代码:

foreach ($topics as $key => $value) {
    $topics[$key]['lessons'] = array_map(function ($lesson) use ($value) {
        
            $lesson['reschedule'] = true;
            return $lesson;
        
    }, $value['lessons']);
}

我想添加' reschedule '键到每一课,但它不工作,为我的财产。
所需输出:

[
     "id" => 1,
     "statusName" => "COMPLETED",
     "lessons" => [
       [
         "id" => 1,
         "statusName" => "COMPLETED",
         "reschedule" => true 
       ],
       [
         "id" => 1,
         "statusName" => "COMPLETED",
         "reschedule" => true 
       ],
     ]
  ]
zd287kbt

zd287kbt1#

$result = array_map(function ($topic) {
    $lessons = $topic['lessons'];
    return array_map(function ($lesson) {
        $lesson['reschedule'] = true;
        return $lesson;
    }, $lessons);
}, $topics)

相关问题