用JSON对象值填充输入字段



首先,抱歉,我不是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] => /resourceslogo1.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)
      {...

但是说实话,这一切都超出了我的理解,因为我只是不太明白我需要遍历所有内容来获得对象中给定的值。

我希望这是有意义的,我为缺乏知识而道歉…

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

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

这应该适用于foreach块

相关内容

  • 没有找到相关文章

最新更新