2014-03-24 76 views
0

首先,道歉,我不是一个PHP专家,无论如何,我正在努力解决这个问题,但是我有一个JSON格式的数据库条目。用JSON填充输入字段对象值

我试图然后从这个对象提取值到一系列的输入框。

使用后:

$response = json_decode($row['responseJSON']); 

其中responseJSON是我的表的名称,我返回以下对象:

stdClass Object ([fields] => stdClass Object ([marketingID] => stdClass Object ([attributes] => stdClass Object ([type] => hidden [id] => marketingID [value] => 0) [value] => 7) [marketingTelephone] => stdClass Object ([attributes] => stdClass Object ([type] => text [class] => form-control [id] => marketingTelephone [required] => true [label] => Telephone) [value] => +44 123 456789) [marketingEmail] => stdClass Object ([attributes] => stdClass Object ([type] => email [class] => form-control [id] => marketingEmail [required] => true [placeholder] => [email protected]) [value] => [email protected])) [files] => stdClass Object ([marketingLogo] => stdClass Object ([name] => logo1.png [path] => /resources\logo1.png [size] => 2408 [mime] => [attributes] => stdClass Object ([id] => marketingLogo [type] => file [label] => Choose File)))) 

和输入框:

<perch:input id="marketingLogo" type="file" label="Choose File" /> 
<perch:input type="text" class="form-control" id="marketingTelephone" required="true" label="Telephone" /> 
<perch:input type="email" class="form-control" id="marketingEmail" required="true" placeholder="[email protected]" /> 

我的问题是,我正在尝试遍历使用的对象:

json_o->marketingID (and various versions) but to no avail. 

我当时想,也许我需要去了解它沿着路线:

foreach ($response as $object) { 
    { 
    foreach ($object as $property=>$value) 
     {... 

,但也很坦白,这一切让我的头,我只是不太明白,我怎么需要遍历所有内容以获得对象中给出的值。

我希望这是有道理的,我对知识的缺乏道歉......

回答

0

如果你解码JSON到数组,而不是对象可能更容易。这可以通过提供第二个参数(真),以作为json_decode实现:

$response = json_decode($row['responseJSON'], true); 

这应该用foreach块工作

+0

谢谢,我想大公,但就是不知道如何正确地询问了每个循环可以深入到我想要的值。 – user3456060

+0

其实你可能根本不需要foreach块。您可以像下面那样访问嵌套元素(例如访问MarketingTelephone的属性):$ response ['fields'] ['marketingTelephone'] ['attributes'] – mesutozer