user558134
user558134

Reputation: 1129

Jquery javascript associative array

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!

Upvotes: 2

Views: 1121

Answers (3)

Rey Rahadian
Rey Rahadian

Reputation: 455

like this

<script type="text/javascript">
  var anArray = [
                    {"index":[
                    {"subindex":"default"},
                    {"subindex":"default"}
                    ]},
                    {"index2":[
                    {"subindex":"default"},
                    {"subindex2":"default"}
                    ]}
                ];
</script>

Upvotes: 1

freakish
freakish

Reputation: 56587

var a = {
  'index': {
    'subindex1': 'default',
    'subindex2': 'default'
  },
  'index2': {
    'subindex1': 'default',
    'subindex2': 'default'
  }
};

Upvotes: 5

mplungjan
mplungjan

Reputation: 178421

JSON Encode

echo json_encode($array);

Upvotes: 4

Related Questions