đây là những gì tôi có trong file php của tôi:Chuyển đổi đối tượng của lớp stdClass để JSON Object
$session = $m->session;
$ session bây giờ là:
object(stdClass)[31]
public 'id' => string '21112' (length=5)
public 'external_id' => string '' (length=0)
public 'sessiongroupid' => string '1843' (length=4)
public 'eventid' => string '5588' (length=4)
public 'order' => string '0' (length=1)
public 'name' => string 'Ferdau Conference' (length=17)
public 'description' => string 'Ferdau Conference' (length=17)
public 'starttime' => string '2013-04-18 18:00:00' (length=19)
public 'endtime' => string '2013-04-18 18:04:00' (length=19)
public 'speaker' => string '' (length=0)
public 'location' => string 'Waregem' (length=7)
public 'mapid' => string '0' (length=1)
public 'xpos' => string '0.000000' (length=8)
public 'ypos' => string '0.000000' (length=8)
public 'maptype' => string 'plan' (length=4)
public 'imageurl' => string '' (length=0)
public 'presentation' => string '' (length=0)
public 'organizer' => string '0' (length=1)
public 'twitter' => string '' (length=0)
public 'allowAddToFavorites' => string '0' (length=1)
public 'allowAddToAgenda' => string '0' (length=1)
public 'votes' => string '0' (length=1)
public 'url' => string 'http://ferdau.be' (length=16)
public 'venueid' => string '0' (length=1)
Tôi muốn gửi tất cả các thông tin vào một chức năng được đặt tên lưu trong tệp javascript của tôi. Như thế này:
echo '<a onclick="save('.$session.')" style="cursor:pointer;" class="metacell">
<img src="'.buildUri("images/icons/favorite.png").'" width="16" />
<span>Add to favorites</span>
</a>';
Khi tôi thử điều này, tôi luôn gặp lỗi chỉ có thể gửi một chuỗi. Làm thế nào tôi có thể chuyển đổi đối tượng này thành thứ mà tôi có thể gửi cho hàm javascript của mình?
Tôi đã thử này không có kết quả: $data = json_encode($session);
Khi tôi làm $data = json_encode((array)$session)
tôi có được điều này:
<a class="metacell" style="cursor:pointer;" ferdau.be","venueid":"0"})"="" \="" 18:04:00","speaker":"","location":"waregem","mapid":"0","xpos":"0.000000","ypos":"0.000000","maptype":"plan","imageurl":"","presentation":"","organizer":"0","twitter":"","allowaddtofavorites":"0","allowaddtoagenda":"0","votes":"0","url":"http:\="" 18:00:00","endtime":"2013-04-18="" conference","starttime":"2013-04-18="" conference","description":"ferdau="" id":"21112","external_id":"","sessiongroupid":"1843","eventid":"5588","order":"0","name":"ferdau="" onclick="save({">
'$ data = json_encode ((mảng) $ session)' –
Có thể trùng lặp. http://stackoverflow.com/questions/4697656/using-json-encode-on-objects-in-php-regardless-of-scope – karmafunk
Trong PHP> = 5.4.0 có một giao diện mới để sắp xếp các đối tượng vào JSON: JsonSerializable – karmafunk