1

I have the following array in php (with var_dump).

array(135) {
  [0]=>
  object(Book)#138 (2) {
    ["id"]=>
    string(36) "93ec4cd6-49a3-4dc6-94f1-00295cc9cdaf"
    ["title"]=>
    string(33) "title 1"
  }
  [1]=>
  object(Book)#139 (2) {
    ["id"]=>
    string(36) "830fe4b8-927d-4a4c-9398-033358d64551"
    ["title"]=>
    string(12) "title 2"
  }
  [2]=>
  object(Book)#140 (2) {
    ["id"]=>
    string(36) "3ed31443-666c-4d20-81c2-067e42047007"
    ["title"]=>
    string(8) "title 3"
  }

I want to echo a json string so I can read it in angular.

I use echo json_encode($books);

But I get an empty string.

Any idea's on how to convert this array to json?


Extra info after first comment by Syscall and trincot:

Book Class

class Book implements JsonSerializable {

    public $id;
    public $title;

    public function __construct($id = '', $title = '') {
        $this->id = $id;
        $this->title = $title;
    }

    public function jsonSerialize() {
        return get_object_vars($this);
    }
}

BookRepository

require_once 'DBHelper.php';
require_once 'Book.php';

class BookRepository {

    public static function getReeksen() {

        $conn = new PDO("mysql:host=" . DBHelper::$DB_SERVERNAME .";dbname=" . DBHelper::$DB_DATABASENAME, DBHelper::$DB_USERNAME, DBHelper::$DB_PASSWORD);
        $sql = "SELECT id, naam FROM BC_Book";

        $books = array();

        $statement = $conn->prepare($sql);

        $statement->execute();

        $result = $statement->fetchAll(PDO::FETCH_CLASS);

        foreach ($result as $db_book) {
            $book = new Book($db_book->id, $db_book->title);
            array_push($books, $book);
        }

       return $books;
    }
}
2
  • 1
    Try json_encode((array)$books); just give a try Commented Feb 3, 2018 at 12:11
  • 1
    this cast doesn't work Commented Feb 3, 2018 at 14:05

1 Answer 1

4

That is because var_dump() displays the object's values, including private fields of objects, but json_encode() does not.

You need to implements JsonSerializable :

class Book implements JsonSerializable {
    private $id ;
    private $title ;
    public function __construct($id,$title) {
        $this->id = $id;
        $this->title = $title;
    }
    public function jsonSerialize()
    {
        return get_object_vars($this);
    }
}
$arr = [
    new Book("93ec4cd6-49a3-4dc6-94f1-00295cc9cdaf","title 1"),
    new Book("830fe4b8-927d-4a4c-9398-033358d64551","title 2"),
];
var_dump(json_encode($arr)); 

outputs :

"[{"id":"93ec4cd6-49a3-4dc6-94f1-00295cc9cdaf","title":"title 1"},{"id":"830fe4b8-927d-4a4c-9398-033358d64551","title":"title 2"}]"
Sign up to request clarification or add additional context in comments.

4 Comments

I added the JsonSerializable to my Book class, but it doens't work.
You have to implements JsonSerializable (so jsonSerialize()) in your Reeks class.
@Filip, edit your question and add the definition of your class.
@Filip No, there are no problem with that. I read your code and I think there is a problem with this : $statement->fetchAll(PDO::FETCH_CLASS); should be PDO::FETCH_OBJ (see fetch manual).

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.