Hodicska Gergely wrote: > Hi! > > > Would this work the same for multidimensional arrays? > Encoding was a special feature needed by me, maybe you don't need it. > Usage: > myArray = array(...); > echo '<script>'.arrayToJsArray($myArray, 'myArray').'</script>'; > > Felho > > --- 8< --- arrayToJsArray.php --- 8< --- > <? > function valueToJsValue($value, $encoding = false) > { > if (!is_numeric($value)) { > $value = str_replace('\\', '\\\\', $value); > $value = str_replace('"', '\"', $value); > $value = '"'.$value.'"'; > } > if ($encoding) { > switch ($encoding) { > case 'utf8' : > return iconv("ISO-8859-2", "UTF-8", $value); > break; > } > } else { > return $value; > } > } > > function arrayToJsArray( $array, $name, $nl = "\n", $encoding = > false ) { > if (is_array($array)) { > $jsArray = $name . ' = new Array();'.$nl; > foreach($array as $key => $value) { > switch (gettype($value)) { > case 'unknown type': > case 'resource': > case 'object': > break; > case 'array': > $jsArray .= arrayToJsArray($value, > $name.'['.valueToJsValue($key, $encoding).']', $nl); > break; > case 'NULL': > $jsArray .= $name.'['.valueToJsValue($key, > $encoding).'] = null;'.$nl; > break; > case 'boolean': > $jsArray .= $name.'['.valueToJsValue($key, > $encoding).'] = '.($value ? 'true' : 'false').';'.$nl; > break; > case 'string': > $jsArray .= $name.'['.valueToJsValue($key, > $encoding).'] = '.valueToJsValue($value, $encoding).';'.$nl; > break; > case 'double': > case 'integer': > $jsArray .= $name.'['.valueToJsValue($key, > $encoding).'] = '.$value.';'.$nl; > break; > default: > trigger_error('Hoppa, egy új típus a PHP-ben? > '.__CLASS__.'::'.__FUNCTION__.'()!', E_USER_WARNING); > } > } > return $jsArray; > } else { > return false; > } > } > ?> > --- 8< --- arrayToJsArray.php --- 8< --- thanks this works perfect! -- -Nick Peters -- PHP General Mailing List (http://www.php.net/) To unsubscribe, visit: http://www.php.net/unsub.php