Return to Snippet

Revision: 4233
at November 10, 2007 18:42 by inkdeep


Initial Code
function array_to_json_string($arraydata) {
		$output = "";
		$output .= "{";
		foreach($arraydata as $key=>$val){
			if (is_array($val)) {
				$output .= "\"".$key."\" : [{";
					foreach($val as $subkey=>$subval){
						$output .= "\"".$subkey."\" : \"".$subval."\",";
					}
				$output .= "}],";
			} else {
				$output .= "\"".$key."\" : \"".$val."\",";
			}
		}
		$output .= "}";
		return $output;
	}

Initial URL

                                

Initial Description
SEE http://json.org/ for PHP JSON Libraries.

grunt knuckle drag brute force ugly escapes for quotes make a php array a json string

Produces trailing commas which some Javascript Librarys can't deal with.*

_*NOOB NOTE [a comma is added at the end of each array key/value set]_

Initial Title
PHP Array To JSON String

Initial Tags
php, array, json

Initial Language
JavaScript