1

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

4
  • Why shouldn't it work? Commented Jul 25, 2013 at 14:09
  • If $skin['Color'] is a non-associative array it will be encoded as a JSON array, and you'll be able to reference its items numerically. Are you getting any errors when you try to do it? Commented Jul 25, 2013 at 14:10
  • I haven't tried it yet, would it work @JanTojnar ? Commented Jul 25, 2013 at 14:10
  • As @freejosh writes, but don't forget arrays are indexed from 0 in JS. Commented Jul 25, 2013 at 14:11

2 Answers 2

1
$.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.

Sign up to request clarification or add additional context in comments.

Comments

0

jQuery's ajax() returns an object when using the json dataType. This works for multi-dimensional objects too.

Can you be a little bit more specific on your question?

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.