2013-07-19 121 views
0

我有一個json字符串,我想解析小對象的數組,我使用解碼器,但它不會幫助,爲什麼這是幸福嗎?無法解碼json字符串

我已定義的變量爲$ cleanforcharacters

$cleanforcharacters = preg_replace('/["{mtwrdayfsasusseto}"]_/', '', $found['newdiscounthours']); 

這是我的輸出

discount_org: "{"day":"8:00","time":"12:00","discount":"10","day":"8:00","time":"12:00","discount":"10"}" 

這是所需的輸出(對象數組)

discount_org: [ 
{ 
day: 0, 
time: 8, 
discount: 10 
}, 
{ 
day: 0, 
time: 14, 
discount: 10 
}, 

這是怎麼我試過

$ arrayOfEmails = json_decode($ cleanforcharacters);

,這就是我現在越來越

discount_org: { 
day: "20", 
time: "12:00", 
discount: "20" 
} 

剩下的就是不出來或者

回答

0

這是因爲你已宣佈爲對象,按鍵被覆蓋的值,而不是設置爲新的值: -

你給了: -

discount_org: "{"day":"8:00","time":"12:00","discount":"10","day":"8:00","time":"12:00","discount":"10"}" 

它應該是: -

discount_org: "[{"day":"8:00","time":"12:00","discount":"10"},{"day":"8:00","time":"12:00","discount":"10"}]" 

然後使用: -

$arrayOfEmails = json_decode($cleanforcharacters,true); 

這會給你正確的結果。

+0

嗯,這不是因爲它是一個字符串,而不是一個對象嗎?如果是這樣,我不認爲解碼器使用字符串 – Zaz