Your IP : 216.73.217.177


Current Path : /home/balossw/www/temp/common/includes/
Upload File :
Current File : /home/balossw/www/temp/common/includes/js.inc.php

<?php
/*---------------------------------------------------------------------------
		File name ............ common/includes/js.inc.php
		Author ............... Antoine Lemoine
		Creation date ........ 02.09.2009
		Modification date .... 22.10.2009
---------------------------------------------------------------------------*/


function array2js($array, $variable_name, $inline=true)
{
	return array2js_r($array,$variable_name,$deep=0,$inline ? '' :"\n");
}

function array2js_r($array, $variable_name, $deep, $separator)
{
	$var = '_'.$deep;
	if (is_array($array) && count($array)>0)
	{
		if (count($array)==1) $var = $variable_name;
		$output = $var.'=[];'.$separator;
		$output2 = $variable_name.'=[';
		$sol2 = true;
		$last_key = -1;
		foreach ($array as $key => $value)
		{
			$name = $var.'['.(is_int($key) ? $key : "'".$key."'").']';
			if (is_array($value))
			{
				$output .= array2js_r($value,$name,$deep+1,$separator);
				$sol2 = false;
			}
			else
			{
				if (is_numeric($value)) $val = $value;
				else if (is_string($value))
				{
					$val = "'".str_replace(array("'","\r\n","\n"),array("\'","\\r\\n","\\r\\n"),$value)."'";
				}
				else if (empty($value)) $val = "''";
				if ($sol2)
				{
					if (is_int($key) && $key==$last_key+1) $output2 .= ($key>0 ? ',' : '').$val;
					else $sol2 = false;
				}
				$output .= $name.'='.$val.';'.$separator;
			}
			$last_key = $key;
		}
		if ($sol2) $output = $output2.'];'.$separator;
		else if (count($array)>1) $output .= $variable_name.'='.$var.';'.$separator;
	}
	else $output = $variable_name.'=[];'.$separator;
	return $output;
}