コード例 #1
0
ファイル: PurJson.php プロジェクト: rintaun/himawari
	/**
	 * Encode a Json while providing encoding functionnalities.
	 * 
	 * Options may contain:
	 * -   *from_encoding*
	 *     /default to "UTF-8"/
	 *     Encoding of the source array
	 * -   *to_encoding*
	 *     /default to "UTF-8"/
	 *     Encoding of the returned string (JSON is serialize in UTF-8 and then encoded)
	 * -   *pretty*
	 *     /default to boolean "false"/
	 *     Format a JSON string with carriage returns and tabulations
	 * 
	 * @param array $array Array to encode
	 * @param array $options[optional] Options used to alter the method behavior
	 * 
	 * @return string Encoded JSON string
	 */
	public static function encode(array $array,array $options=array()){
		if(!isset($options['from_encoding'])) $options['from_encoding'] = 'UTF-8';
		if(!isset($options['to_encoding'])) $options['to_encoding'] = 'UTF-8';
		if(strtoupper($options['from_encoding'])!='UTF-8')
			$array = PurArray::encode($array,'UTF-8',$options['from_encoding']);
		$array = json_encode($array);
		if(isset($options['pretty'])){
			$array = PurJson::pretty($array);
		}
		if(strtoupper($options['to_encoding'])!='UTF-8')
			$array = mb_convert_encoding($array,$options['to_encoding'],'UTF-8');
		return $array;
	}