gpt4 book ai didi

json - 合并预加载结果

转载 作者:行者123 更新时间:2023-12-01 23:42:44 25 4
gpt4 key购买 nike

我想尝试合并预加载的结果:

这是我的结果:

 [{"id":3,"name":"John","email":"john@doe.com","username":"johndoe",
"user_detail":{"address":"anywhere in the world","country":"Somewhere","city":"Somecity","phones":"012-12345","logo":"cute.jpg","created_at":"-0001-11-30 00:00:00","updated_at":"-0001-11-30 00:00:00"}}]

我想要实现的是删除用户详细信息并加入所有的 json 成为一个这样的:

 [{"id":3,"name":"John","email":"john@doe.com","username":"johndoe","address":"anywhere in the world","country":"Somewhere","city":"Somecity","phones":"012-12345","logo":"cute.jpg","created_at":"-0001-11-30 00:00:00","updated_at":"-0001-11-30 00:00:00"}]

我的模型:

/*User Model*/ 
public function user_detail(){
return $this->hasOne('App\UserDetail');
}

/*User Detail Model*/
public function user(){
return $this->belongsTo('App\User');
}

我的 Controller :

$user= User::with('user_detail')->where('username', $username)->get();

有没有合并json的功能?

谢谢

最佳答案

您正在寻找的是数组展平。你可以这样做

    function flatten(array $array) { 
$return = array();
array_walk_recursive($array, function($a,$b) use (&$return) { $return[$b] = $a; });
return $return;
}

然后像这样调用这个函数(如果它在类中,请使用正确的语法调用函数)

$user= User::with('user_detail')->where('username', $username)->get();
$result = flatten($user);

这会给你想要的结果。希望这对您有所帮助!

关于json - 合并预加载结果,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/31743908/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com