【问题标题】:Why json_encode returns empty brackets?为什么 json_encode 返回空括号?
【发布时间】:2016-04-30 20:24:23
【问题描述】:

$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,表格的“文本”列也是如此。可能是什么问题?

【问题讨论】:

    标签: php mysql yii


    【解决方案1】:

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

    您可以查看此 SO 以获得一些建议

    Using json_encode on objects in PHP (regardless of scope)

    【讨论】:

    • 谢谢,我意识到这是一个 yii 模型对象。不是数组。您能否解释一下如何将私有属性转换为“json_encodable”数组?如果您认为这个问题会对社区有所帮助,您可以考虑投赞成票 :)
    • 但是您似乎想要访问活动记录内容.. 为此,Yii 中有很多功能无需像行对象那样访问..
    • 是的 toArray 方法可以做到,但我想编写自己的解决方案。这也将帮助我学习新的东西。不是在寻找一个完整的解决方案,只是关于如何去做的一些指导。
    • 不仅是 toArray .. 查看相关文档 yiiframework.com/doc-2.0/yii-db-activerecord.html 适当函数可用的所有私有属性 .. _attributes by attributes()。或 getAttribuets() _oldAttributes by getOldAttributes 等等..
    • 我试图这样调用get属性:但它的意思是:调用非对象上的成员函数getAttributes()......$notifications = Notifications::return_new ()->getAttributes();...... return_new() 方法的代码如下: return Notifications::find()->where(['is_seen' => 0])->all(); .......请帮助如何使用 getAttributes(),甚至 Yii 文档也没有说明这一点
    猜你喜欢
    • 2015-10-23
    • 1970-01-01
    • 2012-02-08
    • 2017-05-21
    • 2013-10-22
    • 2015-03-27
    • 2016-06-04
    • 1970-01-01
    相关资源
    最近更新 更多