php – 为什么json_encode返回空括号?

前端之家收集整理的这篇文章主要介绍了php – 为什么json_encode返回空括号?前端之家小编觉得挺不错的,现在分享给大家,也给大家做个参考。

$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,因此表格的’text’列也是如此.可能是什么问题?

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

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

Using json_encode on objects in PHP (regardless of scope)

猜你在找的MySQL相关文章