Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Why json_encode returns empty brackets?

Tags:

php

mysql

yii

var_dump of $array returns this :

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
     ................................
     ................................
     ................................

But the json_encode($array) returns [{}, {}, {}]. What I attempted: Tried changing the character encoding of the whole database to utf8_general_ci.

The character encoding is utf8_general_ci for my table and so is for my 'text' column of the table. What could be the issue?

like image 904
Ramesh Pareek Avatar asked Jan 24 '16 14:01

Ramesh Pareek


People also ask

What does json_encode return?

Syntax. The json_encode() function can return a string containing the JSON representation of supplied value. The encoding is affected by supplied options, and additionally, the encoding of float values depends on the value of serialize_precision.

What does the PHP function json_encode () do?

The json_encode() function is used to encode a value to JSON format.

What is json_encode in Javascript?

json_encode(mixed $value , int $flags = 0, int $depth = 512): string|false. Returns a string containing the JSON representation of the supplied value . If the parameter is an array or object, it will be serialized recursively.

What is json_encode and Json_decode in PHP?

JSON data structures are very similar to PHP arrays. PHP has built-in functions to encode and decode JSON data. These functions are json_encode() and json_decode() , respectively. Both functions only works with UTF-8 encoded string data.


1 Answers

The array you show has all the properties as private. this mean that this value are not available outside their class's scope.

you can look at this SO for some suggestion

Using json_encode on objects in PHP (regardless of scope)

like image 163
ScaisEdge Avatar answered Sep 27 '22 16:09

ScaisEdge