Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

json encode is not working with array of objects

I want to convert array of objects to json encoding, I make like this

$allVisits = $mapper->getAllVisits($year, $month);
echo json_encode($allVisits);

and here's is getAllVisists method

 function getAllVisits($year, $month) {
    $where = array(
        'year = ?' => $year,
        'month = ?' => $month
    );
     $resultSet = $this->getDbTable()->fetchAll( $where);
    $visitsEntries = array();
    foreach ($resultSet as $row) {

        $entry = new Visits_Model_Visit();
        $entry->setId($row->visit_id)
                ->setDay($row->day)
                ->setDate($row->date)
                ->setTarget($row->target)
                ->setStatus($row->visit_status)
                ->setTime($row->visit_time);

        $visitsEntries[] = $entry;
    }
    return $visitsEntries;
}

when I echo the size of $allVisits it return correct number of records, but in js the values are received empty like this [{},{},{},{}]

Edit

When I print_r($allVisists) brfore encoding it it returns

Array
(
    [0] => Visits_Model_Visit Object
        (
            [day:private] => sunday
            [date:private] => 2012-03-06
            [target:private] => شسي
            [id:private] => 1
            [status:private] => 0
            [time:private] => 12:00:00
        )

    [1] => Visits_Model_Visit Object
        (
            [day:private] => sunday
            [date:private] => 2012-03-06
            [target:private] => clinnics
            [id:private] => 4
            [status:private] => 0
            [time:private] => 00:00:00
        )

    [2] => Visits_Model_Visit Object
        (
            [day:private] => Tuesday
            [date:private] => 2012-03-06
            [target:private] => clinnics
            [id:private] => 5
            [status:private] => 0
            [time:private] => 00:00:00
        )

    [3] => Visits_Model_Visit Object
        (
            [day:private] => Wednesday
            [date:private] => 2012-03-28
            [target:private] => ??????? ???????
            [id:private] => 7
            [status:private] => 0
            [time:private] => 12:00:00
        )

)
like image 798
palAlaa Avatar asked Mar 13 '12 12:03

palAlaa


People also ask

Can JSON encode arrays?

To convert an array to json in PHP, use the json_encode() function. The json_encode() function is used to encode a value to JSON format. The json_encode() function converts PHP-supported data type into JSON formatted string to be returned due to JSON encode operation.

How do you create an array of objects ready to be encoded as a JSON string?

Creating an Array of JSON Objects We can create an array of JSON object either by assigning a JSON array to a variable or by dynamically adding values in an object array using the . push() operator or add an object at an index of the array using looping constructs like the for loop or while loop.

Can you JSON encode an object in PHP?

json_encode() is a native PHP function that allows you to convert PHP data into the JSON format. The function takes in a PHP object ($value) and returns a JSON string (or False if the operation fails).

What is JSON encode?

jsonencode encodes a given value to a string using JSON syntax. The JSON encoding is defined in RFC 7159. This function maps Terraform language values to JSON values in the following way: Terraform type. JSON type.


2 Answers

You are using json_encode with objects that don't have any public members. json_encode only works on the members it can "see", that's why those are empty.

Since PHP 5.4 you can make use of the JsonSerializable interface to control which data will be offered for json_encode, e.g.:

class Visits_Model_Visit implements JsonSerializable {
    ...
    public function jsonSerialize() {
        return (object) get_object_vars($this);
    }
    ...
}

If you are below 5.4 you can also implement that function w/o extending from the interface and then assigning the correct value manually:

$visitsEntries[] = $entry->jsonSerialize();

Hope this helps.

like image 180
hakre Avatar answered Sep 20 '22 16:09

hakre


As Ray says if your class properties are protected or private, these will not be jsoned.

Since PHP 5.4 instead of using the commented toJson method, you have the ability to specify which data will be serialized implementing the JsonSerializable interface, so json_encode knows how to work on this.

/* PHP >= 5.4 only */
class Visits_Model_Visit implement JsonSerializable {
    public function jsonSerialize()
    {
        return array(
             'day' => $this->day,
             'date' => $this->date,
             'target' => $this->target,
             'id' => $this->id,
             'status' => $this->status,
        );
    }
}
like image 42
arraintxo Avatar answered Sep 16 '22 16:09

arraintxo