2014-01-16 26 views
10

Ich versuche, chinesische Wörter als Schlüssel und ihre englischen Übersetzungen als Werte aus einer Datenbank in ein PHP-Array zu laden, damit ich sie auf der Clientseite in JavaScript verwenden kann. So lade ich die PHP-Schlüssel: Wert-Paare in das Array JavaScript und versuche, die Ergebnisse als Schlüsselwertpaar als solche Ausgabe:konvertieren PHP assoziative Array in JavaScript-Objekt

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

chinesische und englische Worte werden in einer relationalen Datenbank geladen.

PHP:

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

Javascript: Hier möchte ich die $ .each den Schlüssel als String ausgegeben, und kein Nummernindex. Also, wenn ich var words = [<?php echo '"'.implode('","', $wordsArray).'"' ?>]; als Array versucht, ich habe:

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

Wenn ich wirklich bin auf der Suche nach:

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

Also änderte ich words ein Objekt zu sein, so dass $.each konnte Ausgabeschlüssel als String :

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

Welche Fehler wirft: SyntaxError: Unexpected token ,

Antwort

29

Sie können json_encode() verwenden array als json object wie, machen

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

Fast Recht. Zitiere nicht in Anführungszeichen. – Barmar

+0

@Barmar Ja, du hast recht, ich habe Zitate entfernt und 'kommentiert'. –

0

ich viel für eine elegante Lösung sah dieses Problem zu beheben, ohne dabei Dinge zu verändern, über JavaScript oder einfach ersetzen Zitate über preg_replace (für den Fall, dass die Werte würden Zitate enthalten) und es am Ende selbst tun. Auch wenn es zu spät ist, hoffe ich, dass es denjenigen hilft, die nach der gleichen Lösung suchen.

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); 
} 

Nutzung:

$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); 

Ergebnis:

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

ich ein paar Dinge gerade verändert es mehr kompatibel (Linie 3 und 29) zu machen:

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; 
} 
Verwandte Themen