I have this PHP array:
$teams = array(
"img1" => "img/img1.jpg",
"img2" => "img/img2.jpg",
"img3" => "img/img3.jpg",
...
);
Is there any way to get the same array in Javascript? By same array I mean a array with key => values?
There are no associate arrays in javascript, but you can use objects with a key.value pair.
var obj = {};
obj.myNewKey = myNewValue;
You can also refer to these object properties in the following way:
obj['myNewKey'] = myNewValue;
Just keep in mind that although it looks like an associative array, it is actually an object.
There is also this way:
var obj = {
myNewKey:"myNewValue",
"myOtherKey":"myOtherValue"
};
if you wish to have the exact array from your php transfered to your script tag in html you could pass it as a json string in the following way:
<html>
<head>
<script>
// some js code here
var teams = <?php echo json_encode($teams); ?>;
// some other js code
</script>
</head>
<body>
page html content here
</body>
<html>
var varname = in front of it