2

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!

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. Commented Jan 28, 2012 at 15:34

3 Answers 3

5
var a = {
  'index': {
    'subindex1': 'default',
    'subindex2': 'default'
  },
  'index2': {
    'subindex1': 'default',
    'subindex2': 'default'
  }
};
Sign up to request clarification or add additional context in comments.

1 Comment

it's called a hash or object in JS, btw
4

JSON Encode

echo json_encode($array);

Comments

1

like this

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

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.