JSON To Array [PHP]

JSON To Array [PHP]

This is a snippet I’ve had laying around for awhile, and just never thought to share. If you want to render JSON data on the server side, this jsonToArray() function is a great way to do it.

The code:

function jsonToArray($url) {
	$jsonFile = file_get_contents($url);
	return json_decode($jsonFile, true);

What this does is it takes a URL (Most likely an API or some other JSON data URL) and it returns an array. It’s best used as a global function, and can be executed like so…

$yourArray = jsonToArray("http://api.somedomain.com/json");

Bam! You can now easily pull JSON data into an array in your PHP app. Enjoy!


Back to Blog Join Newsletter