2014-01-16 42 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; 
}