使用JSON Object值填充输入字段

时间:2014-03-24 17:41:41

标签: php json forms input

首先,道歉,无论如何我都不是php专家,我正在努力解决这个问题,但我在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] => you@company.com ) [value] => contact@helloworld.com ) ) [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="you@company.com" />

我的问题是,我试图使用以下方法遍历对象:

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

然后我想,也许我需要沿着这条路走下去:

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

但是太残忍地说,这完全是我的头脑,因为我不太明白我需要遍历所有内容以获得对象中给出的值。

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

1 个答案:

答案 0 :(得分:0)

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

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

这应该适用于foreach块