Brownman Revival
Brownman Revival

Reputation: 3850

json encode multidimensional array get value

 Array
(
    [0] => Array
        (
            [sysid] => 1
            [code] => 140101000
            [name] => China
            [parentid] => 1
        )

    [1] => Array
        (
            [sysid] => 2
            [code] => 140102000
            [name] => Japan
            [parentid] => 1
        )

    [2] => Array
        (
            [sysid] => 3
            [code] => 140103000
            [name] => Hongkong
            [parentid] => 1
        )
)

This is my array that i get from my print_r request it is from php it is a multidimensional array that i used json_encode now this is the data i get from success. I want to get all the value of sysid and name from this json to be put in select option how is this possible

I used this code below but i get undefined and i get 4,000 result

PAIR 0: undefined

PAIR 0: undefined

PAIR 1: undefined

PAIR 1: undefined

for (var i = 0; i < data.length; i++) {
    console.log("PAIR " + i + ": " + data[i].sysid);
    console.log("PAIR " + i + ": " + data[i].name);
}

UPDATE

my bad the first one is the print_r this is the json

[{"sysid":"1","code":"140101000","name":"China","parentid":"1"},{"sysid":"2","code":"140102000","name":"Japan","parentid":"1"},
{"sysid":"3","code":"140103000","name":"Hongkong","parentid":"1"}]

ajax is

$.ajax({
    type: 'POST',
    url: '../include/country.php',
    data: {
        id: id
    },
    success: function(data) {
        // the next thing you want to do 
        var obj = data;
        console.log(obj);
        //for (var i = 0; i < data.length; i++) {
        //console.log("PAIR " + i + ": " + data[i].sysid);
        //console.log("PAIR " + i + ": " + data[i].name);
        //}


    }
});

Upvotes: 0

Views: 758

Answers (1)

user3998237
user3998237

Reputation:

$.ajax({
    type: 'GET',
    url: '../include/country.php',
    dataType : "json",
    data: {
        id: id
    },
    success: function(data) {
       for(var i = 0; i < data.length; i++) {
           console.log("PAIR " + i + ": " + data[i].sysid);
           console.log("PAIR " + i + ": " + data[i].name);
       }
    }
});

Upvotes: 1

Related Questions