Welcome to the Treehouse Community

Want to collaborate on code errors? Have bugs you need feedback on? Looking for an extra set of eyes on your latest project? Get support with fellow developers, designers, and programmers of all backgrounds and skill levels here with the Treehouse Community! While you're at it, check out some resources Treehouse students have shared here.

Looking to learn something new?

Treehouse offers a seven day free trial for new students. Get access to thousands of hours of content and join thousands of Treehouse students and alumni in the community today.

Start your free trial


Aaron Agostini
Aaron Agostini
6,285 Points


I'm working with a Javascript library called Highcharts, which recommends using json_encode to pull their options structure into PHP. Is there a particular lesson here at Treehouse that might shed some more light on json_encode? I'm working my way through the PHP track right now, and I wasn't sure if it was coming up in the javascript seciton.

1 Answer

Simon Klit
Simon Klit
1,686 Points

What exactly do you need to know?

json_encode is a native PHP function that was added in 5.2.0. It accepts any type of UTF-8 encoded input except for resources (http://php.net/manual/en/language.types.resource.php), and encodes it to JSON.

It has a very simple syntax. Here is an example using an array:

$arr = array("foo" => "bar", "hello" => "world");
echo json_encode($arr);

Which outputs:


Likewise, you can use json_decode to convert some JSON to a PHP variable.

More information regarding the "options" and "depth" arguments that the function also accepts, can be found here: http://php.net/manual/en/function.json-encode.php