开发者

jquery json multidimensional array lengths

开发者 https://www.devze.com 2022-12-12 09:44 出处:网络
I\'m using a jQuery ajax json call to retrieve a 3 dimensional PHP array into Javascript. the PHP array looks like this:

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.


In PHP, if you do not start an array with the index of 0 it is treated as an associative array. It then becomes an Object in JSON when you use the json_encode method.

There are a number of optimizations you could make to your code, but since you just gave this as an example, I am assuming your real code has a lot more going on.

This is the same code you provided, only the index on the second-tier elements start at zero now instead of one

$rate[0][0]['name'] = 'guff';
$rate[0][0]['value'] = 'puff';
$rate[0][1]['name'] = 'guff';
$rate[0][1]['value'] = 'puff';
$rate[0][2]['name'] = 'guff';
$rate[0][2]['value'] = 'puff';
$rate[1][0]['name'] = 'guff';
$rate[1][0]['value'] = 'puff';
$rate[1][1]['name'] = 'guff';
$rate[1][1]['value'] = 'puff';

Passing it through json_encode yields this:

[[{"name":"guff","value":"puff"},{"name":"guff","value":"puff"},{"name":"guff","value":"puff"}],[{"name":"guff","value":"puff"},{"name":"guff","value":"puff"}]]

Where you can see that the second level elements are still an array in JSON ([]) instead of an object ({}).

0

精彩评论

暂无评论...
验证码 换一张
取 消