This should do the trick!

// convert object => json
$json = json_encode($myObject);

// convert json => object
$obj = json_decode($json);

Here's an example

$foo = new StdClass();
$foo->hello = "world";
$foo->bar = "baz";

$json = json_encode($foo);
echo $json;
//=> {"hello":"world","bar":"baz"}

print_r(json_decode($json));
// stdClass Object
// (
//   [hello] => world
//   [bar] => baz
// )

If you want the output as an Array instead of an Object, pass true to json_decode

print_r(json_decode($json, true));
// Array
// (
//   [hello] => world
//   [bar] => baz
// )    

More about json_encode()

See also: json_decode()

Answer from maček on Stack Overflow
🌐
PHP
php.net › manual › en › function.json-encode.php
PHP: json_encode - Manual
Returns a string containing the JSON representation of the supplied value. If the parameter is an array or object, it will be serialized recursively. If a value to be serialized is an object, then by default only publicly visible properties will be included.
Discussions

passing JSON object through PHP
Create php that echos out your data array, use php json_encode. Then in your JavaScript file just make an Ajax request to the php file url More on reddit.com
🌐 r/PHPhelp
5
0
February 29, 2024
Get JSON object from PHP using javascript - JavaScript - SitePoint Forums | Web Development & Design Community
Hello guys. This might be a silly question. Let’s say I have an array in my PHP file that I encoded to a valid JSON object ready to be passed to my script where I will loop over this. I’m just a bit confused how to get… More on sitepoint.com
🌐 sitepoint.com
0
September 13, 2017
A simple tool i wrote to convert your JSON into PHP classes
Emoji breaks it: {"name😀":"php","name":"php9.0"} More on reddit.com
🌐 r/PHP
42
84
November 10, 2021
JsonMapper: map a JSON response to your PHP objects
I’ve released a library that helps mapping a JSON object (or array of ...) to PHP objects using a series of configurable middleware. This solves my personal pain of every time writing boilerplate code to map an unhinted JSON to a proper type hinted PHP object, to get to a point where the code completion in the IDE is correct. Other projects that for something similar where not as flexible or supporting the modern PHP functions such as typed attributes. As an experiment I’ve made it into an opensource project. I’m curious for your feedback and tips even if you have tips to to get the project known to more people. More on reddit.com
🌐 r/PHP
22
11
May 23, 2020
🌐
W3Schools
w3schools.com › php › php_json.asp
PHP and JSON
PHP has some built-in functions to handle JSON. First, we will look at the following two functions: ... The json_encode() function is used to encode a value to JSON format. This example shows how to encode an associative array into a JSON object:
🌐
Scout APM
scoutapm.com › home › blog › php json_encode: serialize php objects to json
PHP Json_encode: Serialize PHP Objects to JSON | Scout Monitoring
May 1, 2020 - Since the information in JSON is stored in key/value pairs,*** json_encode()*** is more likely to be used to encode PHP objects and their instance variables.
🌐
ReqBin
reqbin.com › code › php › 0kvtdskv › php-array-to-json-example
How do I convert PHP Array to JSON?
To convert a PHP array to JSON data string, you can use the json_encode($value, $flags, $depth) function.
🌐
Medium
medium.com › homullus › deserializing-json-into-objects-in-php-336bd4f699fc
Deserializing JSON into Objects in PHP | by MrManafon | Homullus | Medium
May 30, 2018 - If you are able to ask an Object what Class it is, and are able to ask that Class what methods are supported in which states, or what private variables it has, if you are able to find out that Class extends another class or that there are properly named getters and setters. You can do magical stuff like create a magic getter which in a way changes the structure of PHP others will write after you.
Find elsewhere
🌐
Jonathan Suh
jonsuh.com › blog › convert-loop-through-json-php-javascript-arrays-objects
Convert and Loop through JSON with PHP and JavaScript Arrays/Objects — Jonathan Suh
September 30, 2014 - Note the differences in accessing the values of an array vs an object. PHP also features a json_encode function to convert an array or object into a string.
🌐
ProxyScrape
proxyscrape.com › tools › php-to-json
PHP Array to JSON Converter - ProxyScrape Tools
Free online tool to convert PHP arrays to JSON format. Convert PHP array syntax to valid JSON objects and arrays with proper formatting. Perfect for PHP developers working with APIs.
🌐
Weblite
solutions.weblite.ca › php2json
Serialized PHP <-> JSON Converter | Web Lite Solutions
This converter allows you to convert PHP serialized objects to and from JSON format.
🌐
ReqBin
reqbin.com › code › php › 4qfsaffq › php-json-encode-example
How do I encode a PHP object to JSON string?
July 5, 2023 - To encode a PHP object to a JSON formatted string, you can use the json_encode($value, $options, $depth) function. The $value parameter specifies the PHP object to encode.
🌐
CodeSignal
codesignal.com › learn › courses › handling-json-files-with-php › lessons › parsing-and-accessing-json-data-with-php
Parsing and Accessing JSON Data with PHP
Here, $data['students'] accesses the array, [1] refers to the second object in the array, and ['name'] fetches the value. The echo statement outputs it directly. Running this code results in: ... When working with JSON parsing in PHP, you may face issues.
🌐
Reddit
reddit.com › r/phphelp › passing json object through php
r/PHPhelp on Reddit: passing JSON object through PHP
February 29, 2024 -

Hello everyone, I have the following javascript code in my project:

let questions = [
{
	numb: 1,
	question: "Question 1",
	answer: "1.",
	options: [
		"some text",
		"some text",
		"some text",
		"some text",
		"some text"
	]
},
{
	numb: 2,
	question: "Question 2",
	answer: "2.",
	options: [
		"some text",
		"some text",
		"some text",
		"some text",
		"some text"
	]
}

];

Instead of having this array defined in my .js file, I want to make an XMLHttpRequest to a PHP script which will return this so it can be used in my JS, how can this be achieved? Sorry if this question is not well asked, I can't find the concept/terminology I'm looking for.

🌐
SitePoint
sitepoint.com › javascript
Get JSON object from PHP using javascript - JavaScript - SitePoint Forums | Web Development & Design Community
September 13, 2017 - You may need a separate PHP script file to retrieve the data via AJAX so that it is not polluted with HTML or anything else that is not the JSON, even PHP notices will pollute the response.
🌐
ReqBin
reqbin.com › code › php › 0uwqq41y › json-to-array-php-example
How do I convert JSON to PHP array?
PHP provides many built-in functions for manipulating arrays, including sorting, searching, checking if an element exists in an array, splitting string to array, converting array to string, converting array to JSON, and getting the length of an array. JSON (JavaScript Object Notation) is a ...
🌐
GeeksforGeeks
geeksforgeeks.org › php › how-to-parse-a-json-file-in-php
How to parse a JSON File in PHP? - GeeksforGeeks
PHP offers built-in functions to handle JSON encoding and decoding, making it easy to work with JSON data. The general syntax of a JSON object looks like this:
Published: June 24, 2025
🌐
W3Schools
w3schools.com › js › js_json_server.asp
JSON PHP
The request body must contain text, not a JavaScript object. Use JSON.stringify() to convert the object into JSON text.
🌐
PHPpot
phppot.com › php › json-handling-with-php-how-to-encode-write-parse-decode-and-convert
PHP JSON Encode and Decode with json_encode() and json_decode() - PHPpot
For exact syntax and supported flags, you can always refer to the official PHP documentation for json_encode(). The json_encode() function converts a PHP value into a JSON string. You will usually pass an array or object to it.