2016-01-24 130 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方法做到了,但我想写我自己的解决方案。这也将帮助我学习新的东西。不寻找一个完整的解决方案,只是一些指导如何去做到这一点。 –