Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
715 views
in Technique[技术] by (71.8m points)

hyperf 从数据库中取出的数据 无法遍历修改里面的值

  • 进行数据遍历修改了 status和is_admin两个值
 $resUser = Db::table('admin')->get();
 foreach ($resUser as &$item) {
 $item['status'] = adminStatusType($item['status']);
 $item['is_admin'] = '管理员';
 }
return $resUser;
  • 但是结果还是没有改变
    image.png

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Answer

0 votes
by (71.8m points)
$resUser = $resUser->map(function($item){
    $item->status = adminStatusType($item['status']);
    $item->is_admin = '管理员';
    return $item;
});

var_dump($resUser);

// 或者

$resUser = $resUser->toArray();
foreach ($resUser as &$item) {
    $item['status'] = adminStatusType($item['status']);
    $item['is_admin'] = '管理员';
}
unset($item);

var_dump($resUser);

不过话说回来,从源码里面看,直接改应该也是可以的,可能是引用存在问题。


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...