How to create an associative javascript/jquery array of this php structure:
$array = array(
'index' => array(
'subindex' => 'default',
'subindex' => 'default'
),
'index2' => array(
'subindex2' => 'default',
'subindex2' => 'default'
)
);
Thanks!
How to create an associative javascript/jquery array of this php structure:
$array = array(
'index' => array(
'subindex' => 'default',
'subindex' => 'default'
),
'index2' => array(
'subindex2' => 'default',
'subindex2' => 'default'
)
);
Thanks!
Share Improve this question asked Jan 28, 2012 at 15:23 user558134user558134 1,1296 gold badges26 silver badges38 bronze badges 1-
Javascript does not have associative arrays. It has objects, which can have properties accessible like this:
obj['propName']
, which kind of makes it look like an associative array....but it's not really an array. – Jonathan Commented Jan 28, 2012 at 15:34
3 Answers
Reset to default 5var a = {
'index': {
'subindex1': 'default',
'subindex2': 'default'
},
'index2': {
'subindex1': 'default',
'subindex2': 'default'
}
};
JSON Encode
echo json_encode($array);
like this
<script type="text/javascript">
var anArray = [
{"index":[
{"subindex":"default"},
{"subindex":"default"}
]},
{"index2":[
{"subindex":"default"},
{"subindex2":"default"}
]}
];
</script>