2014-03-24 77 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