如何在PHP中访问stdClass对象

huangapple go评论78阅读模式
英文:

How to access stdclass object in php

问题

如何从下面的代码中访问名称:

如何访问stdclass对象:

stdClass对象
(
    [formdata] => [{"name":"aaa"},{"designation":"","organization":"","country":"","email":"","phone":"","status":"","interest":[],"comment":"","radio":""}]
)
英文:

Can anyone tell me how to access name from below code :

How to access stdclass object

stdClass Object
(
    [formdata] => [{"name":"aaa"},{"designation":""},{"organization":""},{"country":""},{"email":""},{"phone":""},{"status":""},{"interest":[]},{"comment":""},{"radio":""}]
)





答案1

得分: 0

你可以像这样做

$obj = new stdClass();
$obj->formdata = [
    ["name" => "aaa"],
    ["designation" => ""],
    ["organization" => ""],
    ["country" => ""],
    ["email" => ""],
    ["phone" => ""],
    ["status" => ""],
    ["interest" => []],
    ["comment" => ""],
    ["radio" => ""]
];

$newdata = json_decode(json_encode($obj), true);
$data = json_decode($newdata['formdata'], true);
echo '<pre>'; print_r($data);
die;

这将给你一个数组,你可以访问每个变量。

英文:

you can do it like this

$obj = stdClass Object
(
 [formdata] =&gt; [{&quot;name&quot;:&quot;aaa&quot;},{&quot;designation&quot;:&quot;&quot;},{&quot;organization&quot;:&quot;&quot;},{&quot;country&quot;:&quot;&quot;},{&quot;email&quot;:&quot;&quot;},{&quot;phone&quot;:&quot;&quot;},{&quot;status&quot;:&quot;&quot;},{&quot;interest&quot;:[]},{&quot;comment&quot;:&quot;&quot;},{&quot;radio&quot;:&quot;&quot;}]
)

$newdata = json_decode(json_encode($obj), True);
$data =  json_decode($newdata[&#39;formdata&#39;], True);
echo &#39;&lt;pre&gt;&#39;;print_r($data);
 die;

it will give you and array and you can access each variables

huangapple
  • 本文由 发表于 2020年1月6日 15:47:18
  • 转载请务必保留本文链接:https://go.coder-hub.com/59608403.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定