2011-04-22 51 views
0

我試圖插入生成的字符串到後來被用於實施JSON數組的破滅問題將字符串到數組

產生的破滅字符串看起來像這樣

'id' => $this->_SqlResult[0],'UserId' => $this->_SqlResult[1],'Msg' => $this->_SqlResult[2],'MsgStamp' => $this->_SqlResult[3] 

我會想用它在這個代碼

$this->_JsonArr[]=array($Generated string); 

實現這樣的

$this->_JsonArr[]=array('id' => $this->_SqlResult[0],'UserId' => $this->_SqlResult[1],'Msg' => $this->_SqlResult[2],'MsgStamp' => $this->_SqlResult[3]); 

代替我有像這樣

$this->_JsonArr[]=array(" 'id' => $this->_SqlResult[0],'UserId' => $this->_SqlResult[1],'Msg' => $this->_SqlResult[2],'MsgStamp' => $this->_SqlResult[3]"); 

看起來像生成的字符串作爲一個元素作爲鍵和值的對治療。 顯然我可以得到預期的輸出,因爲這一點,任何人都可以幫助我這個

回答

1

爲什麼你需要內爆任何東西?只是通過數組:

$this->_JsonArr[] = your-non-imploded-array-here; 

我認爲一個完整的解決方案,你想做的事是這樣的(即你的問題的第三碼箱):

$row = array(
    'id' => $this->_SqlResult[0], 
    'UserId' => $this->_SqlResult[1], 
    'Msg' => $this->_SqlResult[2], 
    'MsgStamp' => $this->_SqlResult[3] 
); 
$this->_JsonArr[] = $row; 
+1

我認爲(只是瘋狂的猜測)OP在json中提供數據時會感到困惑(因此將數組內嵌入字符串中)。因此,作爲@zul不要忘記'echo json_encode($ this - > _ JsonArr)'輸出帶有數據的字符串。 – 2011-04-22 12:06:33

0

$此 - > _ JsonArr [] = array($ Generated string);

看起來你想要使用數組鍵和值,但我看你投入陣列純字符串與期望陣列分析你的純字符串格式:鍵=>值。

可以嘗試像下面創建陣列:

$這 - > _ JsonArr [$ Generated_key] =陣列($ Generated_value);

(請糾正我,如果我錯了解你的問題)。