Ajax is returning an array and now, how to separate?

1

My Ajax request is returning 3 records from a PHP query (yes, I'm sure), so we have arrays of type:

array 1 ["João", "19/05/1986", "masculino", "Programador", "Campo Grande"]
array 2 ["Maria", "15/05/1988", "feminino", "Enfermeira", "Londres"]
array 3 ["Patricia", "04/11/1980", "feminino", "Servidora Pública", "Fortaleza"]

Question: How to separate this data so that I can get the information from each array separately and continue my system? To help I'll put a piece of code

$.ajax({
    type: "POST",
    dataType: "json",
    url: "<?php bloginfo('template_url'); ?>/scripts/php_functions.php",
    data: {
        idClienteDocumentos: idClienteDocumentos,
        ajax: "true"
    },
    success: function(result) {

            var resultado = JSON.parse(result);

            // preciso montar as arrays cujo contador me confirmou que existem
            $("#Contador").val(resultado[6]);

For those who feel compelled to give it another try, I need to put this within a while PHP.

    
asked by anonymous 04.08.2014 / 14:31

1 answer

1

If you already have an arriving array after the JSON.parse, you can use forEach to mount this table. If you want to do with divs here is a suggestion:

var campos = ['arquivo', 'idCliente', 'titulo', 'descricao', 'arquivo', 'data'];
var tabela = $('<div id="tabela"></div>');
resultado.forEach(function (linha) {
    var linhaDiv = $('<div></div>');
    linha.forEach(function (campo, index) {
        $('<div></div>').addClass(campos[index]).text(campo).appendTo(linhaDiv);
    });
    linhaDiv.appendTo(tabela);
});

It would be interesting if you sent objects directly from PHP, so the fields that would be classes could already come together. I suggest this at the end this answer .

    
04.08.2014 / 15:44