1

I return data from PHP Like that.

$return['fillable'] = [
    'field_one',
    'field_two',
    'field_three',
    'field_four',
    'field_five',
];

$json = json_encode($return);

return $json;

Now in my ajax response, I want to loop fillable array.

$.ajax({
    'type': "GET",
    'global': false,
    'dataType': 'json',
    'url': "/commission-process/"+$('#token').val(),
    'data': {'ajax': true},
    'success': function (data) {

        // how to loop my return array data.

    }
});

My Question is that, I want to loop fillable array which prints like that.

field_one
field_two
field_three
field_four
field_five

Thanks.

Theo
  • 57,719
  • 8
  • 24
  • 41
John Brad
  • 447
  • 1
  • 10
  • 26

2 Answers2

1

Since I didn't know whether you wanted to loop through it in PHP or Javascript, here's a result in both.

PHP: You have to first decode the json like this:

$array = json_decode( $json, true );

Then you can loop through it like through any other array.

Javascript:

for(i = 0; i<data.fillable.length; i++)
{
    console.log(data.fillable[i]);
}
JTinkers
  • 1,631
  • 9
  • 18
0
$.ajax({
'type': "GET",
'global': false,
'dataType': 'json',
'url': "/commission-process/"+$('#token').val(),
'data': {'ajax': true},
'success': function (data) {

    var json = JSON.parse(data);
    $.each(json, function(i,e){
       console.log(i);
       console.log(e);
    });
}

});