将php关联数组转换为javascript对象

我正在尝试将中文单词作为键加载,将它们的英文翻译作为数据库中的值加载到php数组中,以便我可以在JavaScript的客户端使用它们。 所以我将PHP密钥:值对加载到JavaScript数组中,并尝试将结果输出为键值对,如下所示:

stuff : Ni, You stuff : Ta, Him or Her stuff : Wo, I 

中文和英文单词加载在关系数据库中。

PHP

 $wordsArray = array(); while ($row = $sql->fetch_assoc()) { $wordsArray[$row['chinese']] = $row['english']; } 

Javascript :这里我希望$ .each以字符串forms输出密钥,而不是数字索引。 所以,当我尝试var words = []; 作为一个数组,我得到:

 stuff : 0, You stuff : 1, Him or Her stuff : 2, I 

当我真的在寻找:

 stuff : Ni, You stuff : Ta, Him or Her stuff : Wo, I 

所以我将words改为对象,以便$.each可以将键输出为字符串:

 var words = {}; $.each(words, function(key, value) { console.log('stuff : ' + key + ", " + value); }); 

哪个抛出错误: SyntaxError: Unexpected token ,

您可以使用json_encode()将array作为json object如,

 var words = ;// don't use quotes $.each(words, function(key, value) { console.log('stuff : ' + key + ", " + value); }); 

我看了很多优雅的解决方案,以解决这个问题,而无需通过javascript更改内容或只是通过preg_replace替换引号(对于值将包含引号的情况)并最终自己完成。 即使为时已晚,我希望它能帮助那些寻求相同解决方案的人。

 function json_encode_advanced(array $arr, $sequential_keys = false, $quotes = false, $beautiful_json = false) { $output = "{"; $count = 0; foreach ($arr as $key => $value) { if ( isAssoc($arr) || (!isAssoc($arr) && $sequential_keys == true ) ) { $output .= ($quotes ? '"' : '') . $key . ($quotes ? '"' : '') . ' : '; } if (is_array($value)) { $output .= json_encode_advanced($value, $sequential_keys, $quotes, $beautiful_json); } else if (is_bool($value)) { $output .= ($value ? 'true' : 'false'); } else if (is_numeric($value)) { $output .= $value; } else { $output .= ($quotes || $beautiful_json ? '"' : '') . $value . ($quotes || $beautiful_json ? '"' : ''); } if (++$count < count($arr)) { $output .= ', '; } } $output .= "}"; return $output; } function isAssoc(array $arr) { if (array() === $arr) return false; return array_keys($arr) !== range(0, count($arr) - 1); } 

用法:

 $array = [ 'someField' => '"value"', // double quotes for string if needed 'labelField' => '"label"', // double quotes for string if needed 'boolean' => false, 'numeric' => 5, 'render' => [ 'option' => 'function() { console.log("Hello World!"); console.log(\'Hello World!\'); }', ], ]; echo json_encode_advanced($array); 

结果:

 { someField : "value", labelField : "label", boolean : false, numeric : 5, render : { option : function() { console.log("Hello World!"); console.log('Hello World!'); } } } 

我只是更改了一些内容以使其更兼容(第3行和第29行):

 function json_encode_advanced(array $arr, $sequential_keys = false, $quotes = false, $beautiful_json = false) { $output = isAssoc($arr) ? "{" : "["; $count = 0; foreach ($arr as $key => $value) { if (isAssoc($arr) || (!isAssoc($arr) && $sequential_keys == true )) { $output .= ($quotes ? '"' : '') . $key . ($quotes ? '"' : '') . ' : '; } if (is_array($value)) { $output .= json_encode_advanced($value, $sequential_keys, $quotes, $beautiful_json); } else if (is_bool($value)) { $output .= ($value ? 'true' : 'false'); } else if (is_numeric($value)) { $output .= $value; } else { $output .= ($quotes || $beautiful_json ? '"' : '') . $value . ($quotes || $beautiful_json ? '"' : ''); } if (++$count < count($arr)) { $output .= ', '; } } $output .= isAssoc($arr) ? "}" : "]"; return $output; } 

Rohan Kumar的答案非常出色,只使用jQuery来展示他的结果。 您可以更轻松,更小,请参阅下文。

 var words = ; console.dir(words); 

给你一行结果(对象▶),点击▶将打开对象并显示其内容。