You can convert the PHP object to an array, and then use JSON function to encode it. After that, decode it from JavaScript. Here are some basic steps:
Convert the PHP object to an array.
Using
json_encode()to encode that PHP array.Pass JSON-encoded data to PHP
Decode JSON from JavaScript using
JSON.parseor you can usejQuery.parseJSONto do that.
This is an interesting tutorial about passing a JavaScript object to a PHP object. You might find it useful by watching some other featured/related videos.
Hope this helps.
Answer from user743234 on Stack OverflowConvert PHP response Object to Javascript object - Stack Overflow
arrays - Convert PHP object to send it as an javascript object - Stack Overflow
PHP Passing Array of Objects to Javascript - Stack Overflow
Passing a PHP Class object to Javascript and accessing its properties in Javascript function - Stack Overflow
Swap var_export with json_encode and that should do the job:
json_encode($result->toArray()['Labels']);
Documentation: http://php.net/manual/en/function.json-encode.php
Why not use this model?
$arr=array(
array(
'name'=>'test1',
'id'=>'1',
),
array(
'name'=>'test2',
'id'=>'2',
),
array(
'name'=>'test3',
'id'=>'3',
),
);
echo json_encode($arr); ///[{"name":"test1","id":"1"},{"name":"test2","id":"2"},{"name":"test3","id":"3"}]
Often this is done using an Ajax call to your PHP app, and have PHP generate JSON with the data being sent for processing (see json_encode()).
I've also seen PHP programs generate the JavaScript with the data encoded directly in the script, but I would not recommend this approach.
When echoing the javascript in the view, you can use echo json_encode($array) to convert the PHP array to JSON which can then be inserted into a javascript variable.
You can implement the JsonSerializable interface to define what properties you want to make visible when you serialise an object; e.g:
<?php
class User implements \JsonSerializable
{
private $name;
public function __construct(string $name)
{
$this->name = $name;
}
public function jsonSerialize()
{
return [
'name' => $this->name,
];
}
}
echo json_encode(new User('Bob')); // {"name":"Bob"}
This is an idiomatic way to handle object serialisation in PHP in a transparent and implicit manner, and it allows you to define a preferred shape/format for the json respresentation of your object and encapsulate it within its class definition.
It's quite handy when serialising nested object structures, for example, when you were using value objects - here's a (contrived) example:
class Latitude implements \JsonSerializable
{
private $degree;
public function __construct(float $degree)
{
// validation etc.
$this->degree = $degree;
}
public function jsonSerialize(): float
{
return $this->degree;
}
}
class Longitude implements \JsonSerializable
{
private $degree;
public function __construct(float $degree)
{
// validation etc.
$this->degree = $degree;
}
public function jsonSerialize(): float
{
return $this->degree;
}
}
class Coordinate implements \JsonSerializable
{
public function __construct(Latitude $latitude, Longitude $longitude)
{
$this->latitude = $latitude;
$this->longitude = $longitude;
}
public function jsonSerialize(): array
{
return [
'latlng' => [$this->latitude, $this->longitude],
];
}
}
$coordinate = new Coordinate(new Latitude(0), new Longitude(0));
echo json_encode($coordinate); // {"latlng":[0,0]}
Example: https://repl.it/repls/RelievedLightcyanFolders
I'd venture this is because json_encode() scans the public properties of the object and puts those into the results, but in your code you always use getter functions. It's hard to tell without knowing the class definition of the objects you have in $announcementList. Try this:
<?php echo json_encode([
'id' => $value->getId(),
'title' => $value->getTitle(),
'dateAdded' => $value->getDateAdded(),
]); ?>
You can use json_encode() to make array as an json object like,
var words = <?php echo json_encode($wordsArray) ?>;// don't use quotes
$.each(words, function(key, value) {
console.log('stuff : ' + key + ", " + value);
});
For me the simplest way to convert php array to javascript object is to json_encode the php array then JSON.parse it from javascript like this:
<?php
$array = [1 => "one", 2 => "two", 3 => "three"];
?>
<script>
const jsObject = JSON.parse(`<?= json_encode($array) ?>`)
console.log(jsObject) // Object { 1: "one", 2: "two", 3: "three" }
</script>