首页 > 解决方案 > 如何从 Laravel 中的数组中获取特定的数组值

问题描述

我想从我从这段代码中获得的数组中获取特定的数组数据:

$user       = $request->instance()->query('user');
var_dump($user);exit;

结果是:

array(1) { ["user"]=> object(App\Models\User)#332 (27) { ["fillable":protected]=> array(5) { [0]=> string(4) " name" [1]=> string(8) "username" [2]=> string(5) "email" [3]=> string(8) "password" [4]=> string(5) "token" } ["hidden":protected]=> array(2) { [0]=> string(8) "password" [1]=> string(14) "remember_token" } ["casts":protected]=> array (1) { ["email_verified_at"]=> string(8) "datetime" } ["connection":protected]=> string(5) "mysql" ["table":protected]=> string(5) "users " ["primaryKey":protected]=> string(2) "id" ["keyType":protected]=> string(3) "int" ["递增"]=> bool(true) ["with":protected]=> array(0) { } ["withCount":protected]=> array(0) { } ["perPage":protected]=> int( 15) ["exists"]=> bool(true) ["wasRecentlyCreated"]=> bool(false) ["attributes":protected]=> array(15) { ["id"]=> int(3) [ "name"]=> string(14) "Manchesteriyah" ["username"]=> string(14) "manchesteriyah" ["email"]=> string(24) "manchesteriyah@gmail.com" ["facebook"] => 字符串(37)“bool(false) ["attributes":protected]=> array(15) { ["id"]=> int(3) ["name"]=> string(14) "Manchesteriyah" ["username"]=> string(14) "manchesteriyah" ["email"]=> string(24) "manchesteriyah@gmail.com" ["facebook"]=> string(37) "bool(false) ["attributes":protected]=> array(15) { ["id"]=> int(3) ["name"]=> string(14) "Manchesteriyah" ["username"]=> string(14) "manchesteriyah" ["email"]=> string(24) "manchesteriyah@gmail.com" ["facebook"]=> string(37) "https://web.facebook.com/xkionna " ["phone"]=> NULL ["email_verified_at"]=> NULL ["password"]=> string(60) "$2y$10$IrqHE1JfyH0bJ0XD/Hjy.efLg95y/ buTIir0PuXcOqDb1yCSS69Oe" ["image"]=> NULL ["description"]=> NULL ["role"]=> string(1) "1" ["token"]=> string(20) "ymsJxEtFnxdPBWYwlYFw" ["member_expiration" ]=> 字符串(19) "2019-07-08 20:33:29" ["created_at"]=> 字符串(19) "2019-06-08 20:30:25" ["updated_at"]=> 字符串(19) "2019-06-08 20:33:29" } ["original":protected]=> array(15) { ["id"]=> int(3) ["name"]=> string( 14)“曼彻斯特”[“用户名”]=>字符串(14)“manchesteriyah" ["email"]=> string(24) "manchesteriyah@gmail.com" ["facebook"]=> string(37) " https://web.facebook.com/xkionna" ["phone"]=> NULL ["email_verified_at"]=> NULL ["password"]=> string(60) "$2y$10$IrqHE1JfyH0bJ0XD/Hjy.efLg95y/buTIir0PuXcOqDb1yCSS69Oe" ["image"]=> NULL [ "description"]=> NULL ["role"]=> string(1) "1" ["token"]=> string(20) "ymsJxEtFnxdPBWYwlYFw" ["member_expiration"]=> string(19) "2019-07 -08 20:33:29" ["created_at"]=> 字符串(19) "2019-06-08 20:30:25" ["updated_at"]=> 字符串(19) "2019-06-08 20: 33:29" } ["changes":protected]=> array(0) { } ["dates":protected]=> array(0) { } ["dateFormat":protected]=> NULL ["appends": protected]=> 数组(0){ } ["dispatchesEvents":protected]=>array(0) { } ["observables":protected]=> array(0) { } ["relations":protected]=> array(0) { } ["touches":protected]=> array(0) { } ["timestamps"]=> bool(true) ["visible":protected]=> array(0) { } ["guarded":protected]=> array(1) { [0]=> string(1) "*" } ["rememberTokenName":protected]=> 字符串(14) "remember_token" } }记住令牌" } }记住令牌" } }

我想得到的是用户名值,我尝试了一些这样的解决方案:

$user->username

但它给了我这样的错误:

试图获取非对象的属性“用户名”

如何解决这个问题?感谢关注。

标签: phparrayslaravel

解决方案


$user变量似乎包含一个包含 1 个元素的数组,而不是用户本身。您必须首先从数组中检索用户对象,然后访问用户名属性。

$user = $request->instance()->query('user')['user'];
var_dump($user->username); exit;

推荐阅读