I want to convert the following PHP code to JavaScript, This is an array of key/value pairs in PHP:
PHP code:
$countries = array("AF"=>"AFGHANISTAN","AX"=>"ALAND ISLANDS");
What's the best way to do this in jQuery or JavaScript?
I want to convert the following PHP code to JavaScript, This is an array of key/value pairs in PHP:
PHP code:
$countries = array("AF"=>"AFGHANISTAN","AX"=>"ALAND ISLANDS");
What's the best way to do this in jQuery or JavaScript?
Use json_encode():
<script>
var countries = <?php echo json_encode($countries); ?>;
</script>
That's what will be stored in countries:
{
AF : "AFGHANISTAN",
AX : "ALAND ISLANDS"
}
If you just want to reproduce such a data structure in JS, use objects as stated in the other posts.