为什么json_encode返回空括号?

时间:2016-01-24 14:47:58

标签: php mysql yii

$ array的var_dump返回:

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($ array)返回[{},{},{}]。 我的尝试: 尝试将整个数据库的字符编码更改为utf8_general_ci。

我的表格的字符编码为utf8_general_ci,因此我的文字编号为'表的列。可能是什么问题?

1 个答案:

答案 0 :(得分:10)

您显示的数组将所有属性设置为私有。这意味着该值在其类的范围之外是不可用的。

你可以看看这个SO的一些建议

Using json_encode on objects in PHP (regardless of scope)