02 PHP POST  all data,  Save to JSON string

02 PHP POST all data, Save to JSON string

<?php
/*
foreach (array_keys($_POST) as $field)
{
echo $_POST[$field];
}
*/
$jsonString="{";
$i=0;
foreach ($_POST as $name => $val)
{
	try {
		if($i>0){$jsonString=$jsonString.",";}
		$i=$i+1;
		if ( is_array( $val ) == true ) {
			$jsonString = $jsonString."\"" . $name . "\":" . json_encode($val) . "";
		} else {
			if(is_numeric($val)){
				$jsonString = $jsonString."\"" . $name . "\":" . $val . "";
			}else{
				$jsonString = $jsonString."\"" . $name . "\":\"" . $val . "\"";
			}
			// echo htmlspecialchars( $name . ': ' . $val ) . "<br>\n";
		}
	}catch  (Exception $e) {
	}
}
$jsonString=$jsonString."}";
echo $jsonString;


 
?>

output:

{"aaa":"123@gmail.com","bbb":"asds","cccc":"","dddd":["cccc"]}

PHP
01 StringCH02 ArrayCH03 檔案