[英]Getting a specific value from a json response
I have this code我有这个代码
$client = json_decode($client);
echo "<pre>";
print_r($client);
which produces there在那里生产
Array
(
[0] => stdClass Object
(
[id] => 1
[name] => Jojohn@doe.com
[email_verified_at] =>
[password] => $2y$10$pAvJ9/K7ZPOqw10WhfmToumK0TY1XihY8M9uAEEs4GkHZr4LdGc4e
[remember_token] =>
[created_at] => 2020-07-29 21:08:02
[updated_at] =>
[userid] => 2
[account_rep] => 3
)
)
My question is how do I get the value of name and account_rep I tried我的问题是如何获得我尝试过的 name 和 account_rep 的值
echo $client['0']['object']['name'];
but that does not work it just throws an error但这不起作用它只会引发错误
Cannot use object of type stdClass as array
json_decode($variable), is used to decode or convert a JSON object to a PHP object. json_decode($variable),用于将 JSON object 解码或转换为 PHP ZA8ZCFDE6331BD59EB8661C
So you could do this as $client['0']
is an object.所以你可以这样做,因为
$client['0']
是 object。
echo $client['0']->name;
But I'd say you should rather convert the json object to associative array instead of PHP object by passing TRUE
to as an argument to the json_decode. But I'd say you should rather convert the json object to associative array instead of PHP object by passing
TRUE
to as an argument to the json_decode. When TRUE
, returned objects get converted into associative arrays.当
TRUE
时,返回的对象被转换为关联的 arrays。
$client = json_decode($client, true);
Now $client is现在 $client 是
Array
(
[0] => Array
(
[id] => 1
[name] => Jojohn@doe.com
[email_verified_at] =>
[password] => $2y$10$pAvJ9/K7ZPOqw10WhfmToumK0TY1XihY8M9uAEEs4GkHZr4LdGc4e
[remember_token] =>
[created_at] => 2020-07-29 21:08:02
[updated_at] =>
[userid] => 2
[account_rep] => 3
)
)
Now you could simply do现在你可以简单地做
echo $client[0]['name'];
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.