2016-01-24 129 views
2

的var_dump $陣列的返回這樣的:爲什麼json_encode返回空括號?

array (size=3) 
    0 => 
    object(frontend\models\Notifications)[101] 
     private '_attributes' (yii\db\BaseActiveRecord) => 
     array (size=5) 
      'id' => int 1 
      'created_on' => string '2015-11-12 12:12:15' (length=19) 
      'user_id' => int 1 
      'text' => string '2severity level is 2guardian is 5,Student_id 2 created a Level 2 discipline issue in school' (length=91) 
      'is_seen' => int 0 
     private '_oldAttributes' (yii\db\BaseActiveRecord) => 
     array (size=5) 
      'id' => int 1 
      'created_on' => string '2015-11-12 12:12:15' (length=19) 
      'user_id' => int 1 
      'text' => string '2severity level is 2guardian is 5,Student_id 2 created a Level 2 discipline issue in school' (length=91) 
      'is_seen' => int 0 
     private '_related' (yii\db\BaseActiveRecord) => 
     array (size=0) 
      empty 
     private '_errors' (yii\base\Model) => null 
     private '_validators' (yii\base\Model) => null 
     private '_scenario' (yii\base\Model) => string 'default' (length=7) 
     private '_events' (yii\base\Component) => 
     array (size=0) 
      empty 
     private '_behaviors' (yii\base\Component) => 
     array (size=0) 
      empty 
    1 => 
    object(frontend\models\Notifications)[108] 
     private '_attributes' (yii\db\BaseActiveRecord) => 
     array (size=5) 
      'id' => int 2 
      'created_on' => string '2015-11-12 12:12:15' (length=19) 
      'user_id' => int 1 
      'text' => string '2severity level is 2guardian is 5,Student_id 2 created a Level 2 discipline issue in school' (length=91) 
      'is_seen' => int 0 
    ................................ 
    ................................ 
    ................................ 

但json_encode($陣列)返回[{},{},{}]。 我試圖: 試圖改變整個數據庫的字符編碼爲utf8_general_ci。

對於我的表,字符編碼是utf8_general_ci,因此對於表的「文本」列。可能是什麼問題?

回答

5

您顯示的數組的所有屬性都是私有的。這意味着這個值在其課程範圍之外是不可用的。

你可以看看這個SO一些建議

Using json_encode on objects in PHP (regardless of scope)

+0

感謝,我意識到這是一個警予模型對象。不是一個數組。你能解釋一下如何將私有屬性轉換爲'json_encodable'數組?如果你認爲這個問題是要幫助社區,你可能會考慮一個upvote :) –

+0

但似乎你想訪問一個活躍的記錄內容..對於這個在Yii中有很多功能,而不像一個行對象訪問.. – scaisEdge

+0

是的toArray方法做到了,但我想寫我自己的解決方案。這也將幫助我學習新的東西。不尋找一個完整的解決方案,只是一些指導如何去做到這一點。 –