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 am getting from my print_r request. This is php, this is a multidimensional array that I used json_encode

, now this is the data I get from success. I want to get all sysid value and name from this json to be included in the select option as possible

I used this code below but I am getting undefined and I am getting 4000 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 first one is print_r, this is 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

$.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);
        //}


    }
});

      

0


source to share


1 answer


$.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);
       }
    }
});

      



+1


source







All Articles