I'm using a jQuery ajax json call to retrieve a 3 dimensional PHP array into Javascript. the PHP array looks like this:
$rate[0][1]['name'] = 'guff'; $rate[0][1]['value'] = 'puff'; $rate[0][2]['name'] = 'guff'; $rate[0][2]['value'] = 'puff'; $rate[0][3]['name'] = 'guff'; $rate[0][3]['value'] = 'puff'; $rate[1][1]['name'] = 'guff'; $rate[1][1]['value'] = 'puff'; $rate[1][2]['name'] = 'guff'; $rate[1][2]['value'] = 'puff';
I'm trying to find the length of the sub arrays e.g. rate[1].length
when I get this from the jquery ajax call:
$.ajax({ type: 'post', url: 'ajax_load_rates.php', data: 'trip_type='+$('#trip_type').val(), dataType: "json", success: function(dat) { rates = dat; }});
jQuery seems to convert the outer array, so rates.length
gives me a value. But doesn't convert the sub arrays, so rates[9].length
is undefined
I've tried looping through the array to convert it manually:
$(rates).each(function(i) { rates[i] = jQuery.makeArray($(this)); console.log(rates[i].length); });
But I get values of 1, when I know there are loads.
here a sample of the json string:
[{"1":{"name":"Single-trip base loading","value":"10","default":"0"},"2":{"name":"Multi-trip base loading","value":"50","default":"0"}},{"1":{"name":"City break","value":"70","default":"0"},"2":{"name":"Sun and sand","value":"75","default":"0"},"3":{"name":"Backpacker","value":"90","default":"0"},"4":{"name":"Horse riding","value":"120","default":"0"},"5":{"name":"Safari","value":"80","default":"0"},"6":{"name":"Golf","value":"85","default":"0"},"7":{"name":"Surf and sail","value":"140","default":"0"},"8":{"name":"Road runner","value":"130","default":"0"},"9":{"name":"Flotilla","value":"150","default":"0"},"10":{"name":"Offshore sailor","value":"200","default":"0"},"11":{"name":"Kite surfer","value":"300","default":"0"}}]
Any ideas, help would be great. I'm quite new to json/ajax/jQuery. You might be able to tell from my code.
cheers,
rob.