2014-01-16 45 views
10

我试图加载中文单词作为关键字,并将它们的英文翻译作为数据库中的值存入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:在这里,我想$。每个输出键作为一个字符串,而不是数字索引。所以,当我试图var words = [<?php echo '"'.implode('","', $wordsArray).'"' ?>];作为一个数组,我得到:

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 = {<?php echo '"'.implode('","', $wordsArray).'"' ?>}; 
$.each(words, function(key, value) { 
    console.log('stuff : ' + key + ", " + value); 
}); 

会抛出错误:SyntaxError: Unexpected token ,

回答

29

您可以使用json_encode()使array作为json object一样,

var words = <?php echo json_encode($wordsArray) ?>;// don't use quotes 
$.each(words, function(key, value) { 
    console.log('stuff : ' + key + ", " + value); 
}); 
+0

几乎对。不要把它放在引号中。 – Barmar

+0

@Barmar是的,你是对的,我删除了引号和'评论'它。 –

0

我看了很多关于一个优雅的解决方案来解决这个问题而不做了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!'); 
     } 
    } 
} 
0

我只是进行了一些修改,使之更加兼容(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; 
}