Take the 2-minute tour ×
Stack Overflow is a question and answer site for professional and enthusiast programmers. It's 100% free, no registration required.

The webpage I'm developing retrieves (product) information from the database via an ajax call to a php file. Given the array structure and related json encoded nested string in the simplified php file below, how to define the corresponding nested array in javascript in an elegant way?

I looked at the examples such as in JS nested arrays, but still get stuck...

php code:

$productinfo = array();
$productinfo['supplierA']['agreementX']['productY']['productpropertyZ'] = 'valueProductproperty';
echo json_encode($productinfo);
share|improve this question
3  
An array needs integer indices, no? Maybe you want an object? –  Scott Hunter Dec 30 '13 at 17:20
    
What's wrong with the code you have? What's going wrong? –  Pointy Dec 30 '13 at 17:21
    
@scott: Ideally I'd like to use a nested associative array with keys and using the javascript object description –  Joppo Dec 30 '13 at 17:23
    
You're missing a ; at the end of the first line. –  user1389596 Dec 30 '13 at 17:25
    
@Pointy: My current code uses a (complex) two dimensional array, in which the keys in the provided php code are merged. Then I just found out that php allows arrays with more than 2 dimensions (yes, my coding experience is still limited), so I'd like to improve the code... –  Joppo Dec 30 '13 at 17:27

1 Answer 1

up vote 4 down vote accepted

you are trying to create an object, not an array, Arrays are ordered lists, objects are unordered key/value pairs.

This would do the job:

var obj = {"supplierA": {"agreementX": {"productY": {"productpropertyZ":"valueProductproperty"}}}};

with more than one value, this could look like this:

var obj = {
     "A": {
         "1": "asd",
         "2": {
             "I": "asdf",
             "II": "asdfg"
          }
      },
      "B": "asdfgh"
    }

for more info just go to http://json.org/

share|improve this answer
1  
Your first example will actually throw TypeError: Cannot read property 'agreementX' of undefined –  Paul S. Dec 30 '13 at 17:26
    
oh sorry, did not test it, thanks –  bbuecherl Dec 30 '13 at 17:27
1  
Note to OP: JavaScript objects are based on the same idea as PHP associative arrays (a mapping of string keys to values), whereas JavaScript arrays are more akin to a Java or C array (which is a strict mapping of integer indices to values). The terminology can be confusing when mixing languages that have the same terms for slightly different things. –  ajp15243 Dec 30 '13 at 17:30
    
thnx! but how to define the javascript structure if the nested $productinfo array contains more values? (which I intended to ask) –  Joppo Dec 30 '13 at 17:33
    
@bbuecherl: thnx for the added example (!) –  Joppo Dec 30 '13 at 17:43

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.