最新消息:雨落星辰是一个专注网站SEO优化、网站SEO诊断、搜索引擎研究、网络营销推广、网站策划运营及站长类的自媒体原创博客

Jquery javascript associative array - Stack Overflow

programmeradmin0浏览0评论

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
Add a ment  | 

3 Answers 3

Reset to default 5
var 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>
发布评论

评论列表(0)

  1. 暂无评论