Herland Cid Herland Cid - 1 month ago 15
Ajax Question

Get array from php, and get its index in jquery using ajax

I wonder if I can access an array through jQuery by its index like this:

output_string['color'][1]


I actually, build an array of elements like this (it works):

PHP

$arreglo = array(
'color' => $skin['Color'],
'textu' => $skin['ImagenTextura'],
'header' => $skin['Imagen'],
'sombra' => $skin['ImagenSombra'],
'tooltip' => $skin['TooltipColor']
);

echo json_encode($arreglo);


And if I want to get to the file, I get the array like this:

$.ajax({
url: 'ajax.php',
type:'POST',
dataType : 'json',
data: { 'dataString': result },
beforeSend: function(){
$("#loader").show();
},
success: function(output_string){
alert(output_string['color']);
}
});


The problem is that, this time, more than one loop will be loaded, so I need to access it like this:
output_string['color'][1]


Thanks

Answer
$.ajax({
    url: 'ajax.php',
    type: 'POST',
    dataType: 'json',
    data: {},
    beforeSend: function(){
        $("#loader").show();
    },
    success: function(output_string) { 
        $.each(output_string, function (i, item) {
             alert(i + " " +item);
        });
    }
});

If your php is working correctly (as u said) you will get an alert for each result from there.